// SPDX-License-Identifier: GPL-2.0-or-later
/* -*- mode: c; c-basic-offset: 8; -*-
 * vim: noexpandtab sw=8 ts=8 sts=0:
 *
 * Copyright (C) 2004, 2005 Oracle.  All rights reserved.
 */

#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/jiffies.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/bio.h>
#include <linux/blkdev.h>
#include <linux/delay.h>
#include <linux/file.h>
#include <linux/kthread.h>
#include <linux/configfs.h>
#include <linux/random.h>
#include <linux/crc32.h>
#include <linux/time.h>
#include <linux/debugfs.h>
#include <linux/slab.h>
#include <linux/bitmap.h>
#include <linux/ktime.h>
#include <linux/device-mapper.h>
#include <linux/dm-vims.h>
#include <scsi/scsi.h>
#include <scsi/scsi_request.h>
#include <asm/unaligned.h>

#include "heartbeat.h"
#include "tcp.h"
#include "nodemanager.h"
#include "quorum.h"

#include "masklog.h"

static char *g_inconsistent_nodes_info;

/*
 * The first heartbeat pass had one global thread that would serialize all hb
 * callback calls.  This global serializing sem should only be removed once
 * we've made sure that all callees can deal with being called concurrently
 * from multiple hb region threads.
 */
static DECLARE_RWSEM(o2hb_callback_sem);

/*
 * multiple hb threads are watching multiple regions.  A node is live
 * whenever any of the threads sees activity from the node in its region.
 */
static DEFINE_SPINLOCK(o2hb_live_lock);
static DEFINE_MUTEX(o2hb_mutex_lock);
static struct list_head o2hb_live_slots[O2NM_MAX_NODES];
static unsigned long o2hb_live_node_bitmap[BITS_TO_LONGS(O2NM_MAX_NODES)];
static LIST_HEAD(o2hb_node_events);
static DECLARE_WAIT_QUEUE_HEAD(o2hb_steady_queue);
static DECLARE_WAIT_QUEUE_HEAD(o2hb_inflight_io_queue);
static DECLARE_WAIT_QUEUE_HEAD(o2hb_write_thread_queue);

static LIST_HEAD(o2hb_handlers);

#define OCFS2_MAX_HB_CTL_PATH		256
static char ocfs2_hb_ctl_path[OCFS2_MAX_HB_CTL_PATH] = "/usr/sbin/ocfs2_hb_ctl";

/*
 * In global heartbeat, we maintain a series of region bitmaps.
 * 	- o2hb_region_bitmap allows us to limit the region number to max region.
 * 	- o2hb_live_region_bitmap tracks live regions (seen steady iterations).
 * 	- o2hb_quorum_region_bitmap tracks live regions that have seen all nodes
 * 		heartbeat on it.
 * 	- o2hb_failed_region_bitmap tracks the regions that have seen io timeouts.
 */
static unsigned long o2hb_region_bitmap[BITS_TO_LONGS(O2NM_MAX_REGIONS)];
static unsigned long o2hb_live_region_bitmap[BITS_TO_LONGS(O2NM_MAX_REGIONS)];
static unsigned long o2hb_quorum_region_bitmap[BITS_TO_LONGS(O2NM_MAX_REGIONS)];
static unsigned long o2hb_failed_region_bitmap[BITS_TO_LONGS(O2NM_MAX_REGIONS)];

#define O2HB_DB_TYPE_LIVENODES		0
#define O2HB_DB_TYPE_LIVEREGIONS	1
#define O2HB_DB_TYPE_QUORUMREGIONS	2
#define O2HB_DB_TYPE_FAILEDREGIONS	3
#define O2HB_DB_TYPE_REGION_LIVENODES	4
#define O2HB_DB_TYPE_REGION_NUMBER	5
#define O2HB_DB_TYPE_REGION_ELAPSED_TIME	6
#define O2HB_DB_TYPE_REGION_PINNED	7
#define O2HB_DB_TYPE_REGION_INFLIGHT_HB_IO	8
struct o2hb_debug_buf {
	int db_type;
	int db_size;
	int db_len;
	void *db_data;
};

static struct o2hb_debug_buf *o2hb_db_livenodes;
static struct o2hb_debug_buf *o2hb_db_liveregions;
static struct o2hb_debug_buf *o2hb_db_quorumregions;
static struct o2hb_debug_buf *o2hb_db_failedregions;

#define O2HB_DEBUG_DIR			"o2hb"
#define O2HB_DEBUG_LIVENODES		"livenodes"
#define O2HB_DEBUG_LIVEREGIONS		"live_regions"
#define O2HB_DEBUG_QUORUMREGIONS	"quorum_regions"
#define O2HB_DEBUG_FAILEDREGIONS	"failed_regions"
#define O2HB_DEBUG_REGION_NUMBER	"num"
#define O2HB_DEBUG_REGION_ELAPSED_TIME	"elapsed_time_in_ms"
#define O2HB_DEBUG_REGION_PINNED	"pinned"
#define O2STATE_DEBUG_DIR			"o2state"
#define O2HB_DEBUG_NODEDOWN			"node_down"
#define O2HB_DEBUG_INFLIGHT_HB_IO			"inflight_hb_io"

static struct dentry *o2hb_debug_dir;
static struct dentry *o2hb_debug_livenodes;
static struct dentry *o2hb_debug_liveregions;
static struct dentry *o2hb_debug_quorumregions;
static struct dentry *o2hb_debug_failedregions;
static struct dentry *o2state_debug_dir;
static struct dentry *o2hb_debug_nodedown;

static struct kmem_cache *o2hb_inflight_io_cache;

static LIST_HEAD(o2hb_all_regions);

static struct o2hb_callback {
	struct list_head list;
} o2hb_callbacks[O2HB_NUM_CB];

static struct o2hb_callback *hbcall_from_type(enum o2hb_callback_type type);

#define O2HB_DEFAULT_BLOCK_BITS       9

enum o2hb_heartbeat_modes {
	O2HB_HEARTBEAT_LOCAL		= 0,
	O2HB_HEARTBEAT_GLOBAL,
	O2HB_HEARTBEAT_NUM_MODES,
};

static char *o2hb_heartbeat_mode_desc[O2HB_HEARTBEAT_NUM_MODES] = {
	"local",	/* O2HB_HEARTBEAT_LOCAL */
	"global",	/* O2HB_HEARTBEAT_GLOBAL */
};

static int disable_fence;

static int fence_invalid;

static int fence_hb_fault;

static unsigned int invalid_hung_timeout = 180;
module_param(invalid_hung_timeout, int, 0644);
MODULE_PARM_DESC(invalid_hung_timeout, "Update Invalid Hung Timeout");

static unsigned int o2hb_heartbeat_mode = O2HB_HEARTBEAT_LOCAL;

/* disk heartbeat fake dead timeout */
unsigned int o2hb_fake_dead_threshold = O2HB_DEFAULT_FAKE_DEAD_THRESHOLD;
/* disk heartbeat real dead timeout */
unsigned int o2hb_real_dead_threshold = O2HB_DEFAULT_REAL_DEAD_THRESHOLD;

#define MOUNT_CHECK_COUNTS	(o2hb_real_dead_threshold + 10)

/*
 * o2hb_dependent_users tracks the number of registered callbacks that depend
 * on heartbeat. o2net and o2dlm are two entities that register this callback.
 * However only o2dlm depends on the heartbeat. It does not want the heartbeat
 * to stop while a dlm domain is still active.
 */
static unsigned int o2hb_dependent_users;

/*
 * In global heartbeat mode, all regions are pinned if there are one or more
 * dependent users and the quorum region count is <= O2HB_PIN_CUT_OFF. All
 * regions are unpinned if the region count exceeds the cut off or the number
 * of dependent users falls to zero.
 */
#define O2HB_PIN_CUT_OFF		3

/*
 * In local heartbeat mode, we assume the dlm domain name to be the same as
 * region uuid. This is true for domains created for the file system but not
 * necessarily true for userdlm domains. This is a known limitation.
 *
 * In global heartbeat mode, we pin/unpin all o2hb regions. This solution
 * works for both file system and userdlm domains.
 */
static int o2hb_region_pin(const char *region_uuid);
static void o2hb_region_unpin(const char *region_uuid);

/*
 * Leave the group for this filesystem.  This is executed by a userspace
 * program (stored in ocfs2_hb_ctl_path).
 */
static void ocfs2_leave_group(const char *group)
{
	int ret;
	char *argv[5], *envp[3];

	argv[0] = ocfs2_hb_ctl_path;
	argv[1] = "-K";
	argv[2] = "-u";
	argv[3] = (char *)group;
	argv[4] = NULL;

	/* minimal command environment taken from cpu_run_sbin_hotplug */
	envp[0] = "HOME=/";
	envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
	envp[2] = NULL;

	ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
	if (ret < 0) {
		mlog(ML_ERROR,
			"ERROR: ocfs2: Error %d running user helper \"%s %s %s %s\"\n",
			ret, argv[0], argv[1], argv[2], argv[3]);
	}
}

/*
 * Hangup is a required post-umount.  ocfs2-tools software expects the
 * filesystem to call "ocfs2_hb_ctl" during unmount.  This happens
 * regardless of whether the DLM got started, so we can't do it
 * in ocfs2_cluster_disconnect().  The ocfs2_leave_group() function does
 * the actual work.
 */
void ocfs2_cluster_hangup(const char *group, int grouplen)
{
	BUG_ON(group == NULL);
	BUG_ON(group[grouplen] != '\0');

	ocfs2_leave_group(group);
}
EXPORT_SYMBOL_GPL(ocfs2_cluster_hangup);


/* Only sets a new threshold if there are no active regions.
 *
 * No locking or otherwise interesting code is required for reading
 * o2hb_fake_dead_threshold and o2hb_real_dead_threshold
 * as it can't change once regions are active and
 * it's not interesting to anyone until then anyway. */
static void o2hb_fake_dead_threshold_set(unsigned int threshold)
{
	if (threshold > O2HB_MIN_DEAD_THRESHOLD &&
			threshold <= o2hb_real_dead_threshold) {
		spin_lock(&o2hb_live_lock);
		if (list_empty(&o2hb_all_regions))
			o2hb_fake_dead_threshold = threshold;
		spin_unlock(&o2hb_live_lock);
	}
}

static void o2hb_real_dead_threshold_set(unsigned int threshold)
{
	if (threshold >= o2hb_fake_dead_threshold) {
		spin_lock(&o2hb_live_lock);
		if (list_empty(&o2hb_all_regions))
			o2hb_real_dead_threshold = threshold;
		spin_unlock(&o2hb_live_lock);
	}
}

static int o2hb_global_heartbeat_mode_set(unsigned int hb_mode)
{
	int ret = -1;

	if (hb_mode < O2HB_HEARTBEAT_NUM_MODES) {
		spin_lock(&o2hb_live_lock);
		if (list_empty(&o2hb_all_regions)) {
			o2hb_heartbeat_mode = hb_mode;
			ret = 0;
		}
		spin_unlock(&o2hb_live_lock);
	}

	return ret;
}

struct o2hb_node_event {
	struct list_head        hn_item;
	enum o2hb_callback_type hn_event_type;
	struct o2nm_node        *hn_node;
	int                     hn_node_num;
	/* region uuid to fire callbacks per region */
	char                    region_uuid[O2HB_REGION_UUID_LEN + 1];
	u64						hb_generation;
};

#define DOWN_QUORUM_FENCE_SAMPLES 0x3
#define INVALID_QUORUM_FENCE_SAMPLES 0xFFFF
struct o2hb_disk_slot {
	struct o2hb_disk_heartbeat_block *ds_raw_block;
	u16			ds_node_num;
	u64			ds_last_time;
	u64			ds_last_generation;
	u16			ds_equal_samples;
	u16			ds_changed_samples;
	u16			ds_quorum_fence_samples;
	u16			ds_inconsist_samples;
	int			ds_fake_timeout;
	int			conn_query_done;
	int			ds_pre_checked;  /* check if node is in region */
	/* receive generation by net, so it may be changed */
	int			ds_generation_changed;
	spinlock_t	ds_lock;
	struct o2hb_region *reg;
	struct work_struct conn_failure_query;
	struct work_struct ds_pre_check_node;
	struct list_head	ds_live_item;
	bool multicluster_checking;
	bool has_been_fenced;
};

/* each thread owns a region.. when we're asked to tear down the region
 * we ask the thread to stop, who cleans up the region
 *     hr_disk_timeout: disk timeout flag when o2hb_thread exit during mounting
 */
struct o2hb_region {
	struct config_item	hr_item;
	u32 key; /* crc of region uuid */

	struct list_head	hr_all_item;
	struct list_head	hr_inflight_io;
	unsigned		hr_unclean_stop : 1,
				hr_item_pinned : 1,
				hr_item_dropped : 1,
				hr_fence : 1,
				hr_disk_timeout : 1,
				hr_node_deleted : 1,
				hr_multi_cluster : 1,
				hr_inconsistency : 1,
				hr_ltype_inconsistency : 1;

	/* protected by the hr_callback_sem */
	struct task_struct *hr_task;
	struct task_struct *hr_write_task;

	unsigned int		hr_blocks;
	unsigned long long	hr_start_block;

	unsigned int		hr_block_bits;
	unsigned int		hr_block_bytes;

	unsigned int		hr_slots_per_page;
	unsigned int		hr_num_pages;

	struct page **hr_slot_data;
	struct block_device *hr_bdev;
	/* malloc each o2hb_disk_slot separately
	 * to avoid big continuous memory allocation */
	struct o2hb_disk_slot **hr_slots;

	/* live node map of this region */
	unsigned long		hr_live_node_bitmap[BITS_TO_LONGS(O2NM_MAX_NODES)];
	/* copy of dlm_domain_map, used for distinguishing node failure
	 * and network partition when umount and node fake timeout occur
	 * at the same time */
	unsigned long		hr_dlm_domain_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
	int					hr_heartbeating;
	unsigned int		hr_region_num;

	struct dentry *hr_debug_dir;
	struct dentry *hr_debug_livenodes;
	struct dentry *hr_debug_regnum;
	struct dentry *hr_debug_elapsed_time;
	struct dentry *hr_debug_pinned;
	struct dentry *hr_debug_inflight_hb_io;
	struct o2hb_debug_buf *hr_db_livenodes;
	struct o2hb_debug_buf *hr_db_regnum;
	struct o2hb_debug_buf *hr_db_elapsed_time;
	struct o2hb_debug_buf *hr_db_pinned;
	struct o2hb_debug_buf *hr_db_inflight_hb_io;

	/* let the person setting up hb wait for it to return until it
	 * has reached a 'steady' state.  This will be fixed when we have
	 * a more complete api that doesn't lead to this sort of fragility. */
	atomic_t		hr_steady_iterations;

	/* let the o2hb_thread knows that o2hb_write_thread has been
	 * working well. Then o2hb_thread can be launched. The aim of this
	 * rule is to make coding easier and more clear. */
	atomic_t		hr_own_slot_populated;

	/* let the o2hb_thread knows that o2hb_write_thread has exited.
	 * Then o2hb_thread can exit too. The aim of this rule is to make
	 * coding easier and more clear. */
	atomic_t		hr_write_hb_task_exited;

	char			hr_dev_name[BDEVNAME_SIZE];

	unsigned int		hr_timeout_ms;

	/* randomized as the region goes up and down so that a node
	 * recognizes a node going up and down in one iteration */
	u64			hr_generation;

	/* disk heartbeat fake timeout work queue */
	struct delayed_work	hr_write_fake_timeout_work;
	unsigned long		hr_last_fake_timeout_start;
	/* disk heartbeat real timeout work queue */
	struct delayed_work	hr_write_real_timeout_work;
	unsigned long		hr_last_real_timeout_start;

	struct task_struct *hb_timeout_task;

	/* Used during o2hb_check_slot to hold a copy of the block
	 * being checked because we temporarily have to zero out the
	 * crc field. */
	struct o2hb_disk_heartbeat_block *hr_tmp_block;

	/* Used to protect hr_conn_failure_bm, hr_conn_failure_votes,
	 * hr_dlm_domain_map, hr_live_node_bitmap */
	spinlock_t      hr_lock;
	/* change two dimension array to one,
	 * to avoid allocating large memory at one time
	 */
	unsigned long *hr_conn_failure_bm[O2NM_MAX_NODES];
	int hr_conn_failure_votes[O2NM_MAX_NODES];
	struct workqueue_struct *o2hb_worker;

	/* multi volume situation, make decision based on region */
	u16 hr_qs_effect_nodes[O2NM_MAX_NODES];
	u16 hr_qs_lowest_node[O2NM_MAX_NODES];

	struct dentry *hr_debug_o2state;
	u8 o2nodes_state[O2NM_MAX_NODES];

	unsigned long		hr_timeout_map[BITS_TO_LONGS(O2NM_MAX_NODES)];

	/*
	 * Used to prevent from mounting when other
	 * nodes do not catch my heartbeat
	 */
	unsigned long		hr_inconsist_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
	int inconsistency_times;

	/* check if make decision */
	unsigned long hr_quorum_bitmap[BITS_TO_LONGS(O2NM_MAX_NODES)];
	/* lock type dlm or adl */
	bool lock_type_adl;
	bool mount_with_fence;
};

struct o2hb_async_write_io_list {
	struct page *page;
	struct list_head list;
	struct o2hb_region *reg;
};

static void o2hb_run_event_list(struct o2hb_node_event *queued_event);
static void o2hb_queue_node_event(struct o2hb_node_event *event,
				  enum o2hb_callback_type type,
				  struct o2nm_node *node,
				  int node_num,
				  const char *uuid,
				  u64 hb_generation);

static int o2hb_send_one_query(u16 err_node, u16 target_node, int *response);
static int o2hb_inflight_io_empty(struct o2hb_region *reg);

/* refer to ocfs2_dlm_init we know region uuid == domain */
void o2hb_clear_domain_map(const char *domain_name, u16 node_num)
{
	struct o2hb_region *reg;

	spin_lock(&o2hb_live_lock);
	list_for_each_entry(reg, &o2hb_all_regions, hr_all_item) {
		if (reg->hr_item_dropped || !config_item_name(&reg->hr_item))
			continue;
		if (!strncmp(domain_name, config_item_name(&reg->hr_item),
				strlen(config_item_name(&reg->hr_item)))) {
			clear_bit(node_num, reg->hr_dlm_domain_map);
			break;
		}
	}
	spin_unlock(&o2hb_live_lock);
}
EXPORT_SYMBOL_GPL(o2hb_clear_domain_map);

void o2hb_set_domain_map(const char *domain_name, u16 node_num)
{
	struct o2hb_region *reg;

	spin_lock(&o2hb_live_lock);
	list_for_each_entry(reg, &o2hb_all_regions, hr_all_item) {
		if (reg->hr_item_dropped || !config_item_name(&reg->hr_item))
			continue;
		if (!strncmp(domain_name, config_item_name(&reg->hr_item),
				strlen(config_item_name(&reg->hr_item)))) {
			set_bit(node_num, reg->hr_dlm_domain_map);
			break;
		}
	}
	spin_unlock(&o2hb_live_lock);
}
EXPORT_SYMBOL_GPL(o2hb_set_domain_map);

int o2hb_test_domain_map_by_key(u16 node_num, u32 key)
{
	struct o2hb_region *reg;
	int status = 0;

	spin_lock(&o2hb_live_lock);
	list_for_each_entry(reg, &o2hb_all_regions, hr_all_item) {
		if (reg->hr_item_dropped || !config_item_name(&reg->hr_item))
			continue;

		if ((key == reg->key)
				&& (test_bit(node_num, reg->hr_dlm_domain_map))) {
			status = 1;
			break;
		}
	}
	spin_unlock(&o2hb_live_lock);

	return status;
}

static int o2hb_conn_check_err(struct o2hb_region *reg, u16 nd_num)
{
	int i, conn_err = 0;
	unsigned long flags;

	if (nd_num >= O2NM_MAX_NODES) {
		conn_err = 1;
		return conn_err;
	}

	spin_lock_irqsave(&reg->hr_lock, flags);
	for (i = 0; i < O2NM_MAX_NODES; i++) {
		if (nd_num != i) {
			if (reg->o2nodes_state[i] == NODE_STATE_CONNERR) {
				conn_err = 1;
				break;
			}
		}
	}
	spin_unlock_irqrestore(&reg->hr_lock, flags);

	return conn_err;
}

static void o2hb_node_state_change(struct o2hb_region *reg,
			   u16 nd_num, u8 nd_state)
{
	int local_node_num;
	u8 pre_state;
	u8 local_node_pre_state;
	unsigned long flags;

	if (reg == NULL)
		return;

	if (nd_num >= O2NM_MAX_NODES)
		return;

	local_node_num = o2nm_this_node();
	spin_lock_irqsave(&reg->hr_lock, flags);
	pre_state = reg->o2nodes_state[nd_num];
	local_node_pre_state = reg->o2nodes_state[local_node_num];
	reg->o2nodes_state[nd_num] = nd_state;
	spin_unlock_irqrestore(&reg->hr_lock, flags);

	if (nd_state == NODE_STATE_CONNERR) {
		spin_lock_irqsave(&reg->hr_lock, flags);
		reg->o2nodes_state[local_node_num] = NODE_STATE_CONNERR;
		spin_unlock_irqrestore(&reg->hr_lock, flags);
	} else {
		if (pre_state == NODE_STATE_CONNERR && nd_num != local_node_num) {
			if (local_node_pre_state == NODE_STATE_CONNERR &&
					 !o2hb_conn_check_err(reg, local_node_num)) {
				spin_lock_irqsave(&reg->hr_lock, flags);
				reg->o2nodes_state[local_node_num] = NODE_STATE_MOUNTED;
				spin_unlock_irqrestore(&reg->hr_lock, flags);
			}
		}
	}
	return;
}

void o2hb_conn_state_change(u16 nd_num, u8 nd_state)
{
	struct o2hb_region *reg;

	if (nd_state != NODE_STATE_CONNERR && nd_state != NODE_STATE_MOUNTED)
		return;

	spin_lock(&o2hb_live_lock);
	list_for_each_entry(reg, &o2hb_all_regions, hr_all_item) {
		if (reg->hr_item_dropped || !config_item_name(&reg->hr_item))
			continue;
		if (reg->o2nodes_state[nd_num] == NODE_STATE_UMOUNT ||
				reg->o2nodes_state[nd_num] == NODE_STATE_FENCE) {
			continue;
		}

		o2hb_node_state_change(reg, nd_num, nd_state);

	}
	spin_unlock(&o2hb_live_lock);

}

static void o2hb_disk_state_change(struct o2hb_region *reg,
			   struct o2hb_disk_slot *slot)
{
	int nd_num, nd_state;

	nd_num = slot->ds_node_num;
	nd_state = reg->o2nodes_state[nd_num];

	if (nd_num == o2nm_this_node()) {
		switch (nd_state) {
		case NODE_STATE_FAKETIMEOUT:
			o2hb_node_state_change(reg, nd_num, NODE_STATE_MOUNTED);
			break;

		case NODE_STATE_QUORUM:
			o2hb_node_state_change(reg, nd_num, NODE_STATE_QUORUM_DELAY_2S);
			break;

		case NODE_STATE_QUORUM_DELAY_2S:
			o2hb_node_state_change(reg, nd_num, NODE_STATE_MOUNTED);
			break;

		default:
			break;
		}
	} else {
		switch (nd_state) {
		case NODE_STATE_MOUNTED:
			if (slot->ds_equal_samples >= o2hb_fake_dead_threshold &&
					slot->ds_equal_samples < o2hb_real_dead_threshold)
				o2hb_node_state_change(reg, nd_num, NODE_STATE_FAKETIMEOUT);
			break;

		case NODE_STATE_FAKETIMEOUT:
			if (slot->ds_equal_samples < o2hb_fake_dead_threshold)
				o2hb_node_state_change(reg, nd_num, NODE_STATE_MOUNTED);
			break;

		default:
			break;
		}
	}
}

static int o2hb_conn_down(struct o2hb_region *reg, u16 node)
{
	int i, conn_down = 1;
	unsigned long flags;

	spin_lock_irqsave(&reg->hr_lock, flags);

	if (node == o2nm_this_node()) {
		if (reg->hr_heartbeating == 1) {
			conn_down = 0;
			goto out;
		}

		if (!test_bit(node, reg->hr_dlm_domain_map)) {
			conn_down = 0;
			goto out;
		}

		for (i = 0; i < O2NM_MAX_NODES; i++) {
			if (i == node)
				continue;
			if (test_bit(i, reg->hr_live_node_bitmap) &&
					(!o2quo_conn_check_err(i) ||
					!test_bit(i, reg->hr_dlm_domain_map))) {
				conn_down = 0;
				goto out;
			}
		}
	} else {
		if (reg->hr_conn_failure_votes[node] != reg->hr_heartbeating - 1) {
			conn_down = 0;
			goto out;
		}

		for (i = 0; i < O2NM_MAX_NODES; i++) {
			/* Only when all nodes alive tell me that node is dead, I can
			 * make the right judgment */
			if (i == node)
				continue;
			if (test_bit(i, reg->hr_live_node_bitmap) &&
					!test_bit(i, reg->hr_conn_failure_bm[node])) {
				conn_down = 0;
				goto out;
			}
		}
	}

out:
	spin_unlock_irqrestore(&reg->hr_lock, flags);
	return conn_down;
}

static int o2hb_fake_timeout(void *data)
{
	int failed, quorum;
	int node_num;
	struct o2hb_disk_slot *slot;
	u64 cputime;
	struct o2hb_region *reg = data;
	/* the maximum times by which a node checks the network connection
	 * condition between a disk heartbeat fake dead timeout and a disk
	 * heartbeat real dead timeout */
	int network_check_num =
			o2hb_real_dead_threshold - o2hb_fake_dead_threshold;

	mlog(ML_HEARTBEAT|ML_KTHREAD, "%s: fake timeout thread running\n",
			config_item_name(&reg->hr_item));
	mlog(ML_ERROR,
			"Heartbeat write fake timeout to device %s after %u milliseconds. UUID: %s\n",
			reg->hr_dev_name,
			jiffies_to_msecs(jiffies - reg->hr_last_fake_timeout_start),
			config_item_name(&reg->hr_item));

	if (o2hb_global_heartbeat_active()) {
		spin_lock(&o2hb_live_lock);
		if (test_bit(reg->hr_region_num, o2hb_quorum_region_bitmap))
			set_bit(reg->hr_region_num, o2hb_failed_region_bitmap);
		failed = bitmap_weight(o2hb_failed_region_bitmap,
					O2NM_MAX_REGIONS);
		quorum = bitmap_weight(o2hb_quorum_region_bitmap,
					O2NM_MAX_REGIONS);
		spin_unlock(&o2hb_live_lock);

		mlog(ML_HEARTBEAT, "Number of regions %d, failed regions %d\n",
		     quorum, failed);

		/*
		 * Fence if the number of failed regions >= half the number
		 * of  quorum regions
		 */
		if ((failed << 1) < quorum)
			goto out;
	}

	node_num = o2nm_this_node();
	slot = reg->hr_slots[node_num];
	cputime = slot->ds_last_time;

	o2hb_node_state_change(reg, node_num, NODE_STATE_FAKETIMEOUT);

	while (!kthread_should_stop() && network_check_num
			&& slot->ds_last_time == cputime) {
		/*
		 * slot->ds_last_time is used to notify that the disk heartbeat is
		 * restoring when it is changed by "o2hb_check_slot", so here if
		 * the slot->ds_last_time changes, it means the disk heartbeat
		 * recovers and we do not check the network condition any more
		 */
		if (o2hb_conn_down(reg, node_num)) {
			mlog(ML_ERROR, "%s: Disk already timeout, and now network fault\n",
					config_item_name(&reg->hr_item));
			o2hb_node_state_change(reg, node_num, NODE_STATE_FENCE);

			reg->hr_fence = 1;
			if (o2nm_fence_method_is_invalid()) {
				mlog(ML_ERROR,
						"%s: This region should fence self by setting invalid!\n",
						config_item_name(&reg->hr_item));
				fence_invalid = 1;
				o2hb_handle_invalid(config_item_name(&reg->hr_item));
				break;
			} else {
				o2quo_disk_timeout();
			}
		}
		msleep_interruptible(O2HB_REGION_TIMEOUT_MS);
		network_check_num--;
	}

out:
	mlog(ML_HEARTBEAT|ML_KTHREAD, "fake timeout thread exiting\n");
	return 0;
}

static void o2hb_write_fake_timeout(struct work_struct *work)
{
	struct task_struct *timeout_task;
	struct o2hb_region *reg =
			container_of(work, struct o2hb_region,
					hr_write_fake_timeout_work.work);
	unsigned long flags;

	/* if another timeout thread running, force to stop and free it */
	spin_lock_irqsave(&reg->hr_lock, flags);
	timeout_task = reg->hb_timeout_task;
	reg->hb_timeout_task = NULL;
	spin_unlock_irqrestore(&reg->hr_lock, flags);
	if (timeout_task) {
		kthread_stop(timeout_task);
		put_task_struct(timeout_task);
	}

	timeout_task = kthread_create(o2hb_fake_timeout, reg, "o2hb_to-%s",
			reg->hr_item.ci_name);
	if (IS_ERR(timeout_task)) {
		mlog(ML_ERROR, "%s: Create o2hb_fake_timeout thread error\n",
				config_item_name(&reg->hr_item));
		return;
	}
	/* get reference to prevent from NULL pointer dereference
	 * when o2hb_thread stop hb_timeout_task */
	get_task_struct(timeout_task);
	wake_up_process(timeout_task);

	spin_lock_irqsave(&reg->hr_lock, flags);
	reg->hb_timeout_task = timeout_task;
	spin_unlock_irqrestore(&reg->hr_lock, flags);
}

static void o2hb_write_real_timeout(struct work_struct *work)
{
	int failed, quorum;
	struct o2hb_region *reg =
			container_of(work, struct o2hb_region,
					hr_write_real_timeout_work.work);
	struct o2hb_node_event event = {
		.hn_item = LIST_HEAD_INIT(event.hn_item),
	};
	struct o2nm_node *node;

	mlog(ML_ERROR,
			"Heartbeat write real_timeout to device %s after %u milliseconds. UUID: %s\n",
			reg->hr_dev_name,
			jiffies_to_msecs(jiffies - reg->hr_last_real_timeout_start),
			config_item_name(&reg->hr_item));

	if (o2hb_global_heartbeat_active()) {
		spin_lock(&o2hb_live_lock);
		if (test_bit(reg->hr_region_num, o2hb_quorum_region_bitmap))
			set_bit(reg->hr_region_num, o2hb_failed_region_bitmap);

		failed = bitmap_weight(o2hb_failed_region_bitmap,
				O2NM_MAX_REGIONS);
		quorum = bitmap_weight(o2hb_quorum_region_bitmap,
				O2NM_MAX_REGIONS);
		spin_unlock(&o2hb_live_lock);

		mlog(ML_HEARTBEAT, "Number of regions %d, failed regions %d\n",
				quorum, failed);

		/*
		 * Fence if the number of failed regions >= half the number
		 * of quorum regions
		 */
		if ((failed << 1) < quorum)
			return;
	}

	node = o2nm_get_node_by_num(o2nm_this_node());
	if (!node) {
		mlog(ML_ERROR, "%s: Node not exist!\n", config_item_name(&reg->hr_item));
		return;
	}

	mutex_lock(&o2hb_mutex_lock);
	spin_lock(&o2hb_live_lock);
	if (reg->hr_unclean_stop == 1) {
		spin_unlock(&o2hb_live_lock);
		mutex_unlock(&o2hb_mutex_lock);
		o2nm_node_put(node);
		mlog(ML_NOTICE, "region %s already invalid\n", config_item_name(&reg->hr_item));
		return;
	}
	/* queue disk timeout callback event to
	 * set ocfs2 invalid state */
	o2hb_queue_node_event(&event, O2HB_DISK_TIMEOUT_CB, node,
			o2nm_this_node(), config_item_name(&reg->hr_item), reg->hr_generation);
	spin_unlock(&o2hb_live_lock);
	o2hb_run_event_list(&event);

	o2hb_disk_timeout(config_item_name(&reg->hr_item));
	mutex_unlock(&o2hb_mutex_lock);
	o2nm_node_put(node);
}

static void o2hb_arm_write_timeout(struct o2hb_region *reg)
{
	mlog(ML_HB_BIO,
			"%s: Queue write fake timeout for %u ms and write real timeout for %u ms\n",
			config_item_name(&reg->hr_item),
			O2HB_WRITE_FAKE_TIMEOUT_MS, O2HB_WRITE_REAL_TIMEOUT_MS);

	if (o2hb_global_heartbeat_active()) {
		spin_lock(&o2hb_live_lock);
		clear_bit(reg->hr_region_num, o2hb_failed_region_bitmap);
		spin_unlock(&o2hb_live_lock);
	}

	cancel_delayed_work(&reg->hr_write_fake_timeout_work);
	reg->hr_last_fake_timeout_start = jiffies;
	queue_delayed_work(reg->o2hb_worker, &reg->hr_write_fake_timeout_work,
			msecs_to_jiffies(O2HB_WRITE_FAKE_TIMEOUT_MS));

	cancel_delayed_work(&reg->hr_write_real_timeout_work);
	reg->hr_last_real_timeout_start = jiffies;
	queue_delayed_work(reg->o2hb_worker, &reg->hr_write_real_timeout_work,
			msecs_to_jiffies(O2HB_WRITE_REAL_TIMEOUT_MS));
}

static void o2hb_disarm_write_timeout(struct o2hb_region *reg)
{
	cancel_delayed_work_sync(&reg->hr_write_fake_timeout_work);
	cancel_delayed_work_sync(&reg->hr_write_real_timeout_work);
}

/*
 * traverse the heartbeat io list
 */
static int o2hb_traverse_hb_io(struct o2hb_region *reg, char *buf)
{
	int i = 1;
	int out = 0;
	struct o2hb_async_write_io_list *w_io;
	unsigned long flags;
	struct o2hb_disk_heartbeat_block *hb_block;

	spin_lock_irqsave(&reg->hr_lock, flags);
	list_for_each_entry_reverse(w_io, &reg->hr_inflight_io, list) {
		hb_block = page_address(w_io->page);

		out += scnprintf(buf + out, PAGE_SIZE - out, "heartbeat io[%d]  ", i);
		out += scnprintf(buf + out, PAGE_SIZE - out, "cputime : %llu\n", hb_block->hb_seq);
		i++;
	}
	spin_unlock_irqrestore(&reg->hr_lock, flags);

	return out;
}

static inline void o2hb_io_wait_init(struct o2hb_io_wait_ctxt *wc)
{
	atomic_set(&wc->wc_num_reqs, 1);
	init_completion(&wc->wc_io_complete);
	wc->wc_error = 0;
}

/* Used in error paths too */
static inline void o2hb_io_wait_dec(struct o2hb_io_wait_ctxt *wc,
				     unsigned int num)
{
	/* sadly atomic_sub_and_test() isn't available on all platforms.  The
	 * good news is that the fast path only completes one at a time */
	while (num--) {
		if (atomic_dec_and_test(&wc->wc_num_reqs)) {
			BUG_ON(num > 0);
			complete(&wc->wc_io_complete);
		}
	}
}

static void o2hb_wait_on_io(struct o2hb_io_wait_ctxt *wc)
{
	o2hb_io_wait_dec(wc, 1);
	wait_for_completion(&wc->wc_io_complete);
}

static void o2hb_bio_end_io(struct bio *bio)
{
	struct o2hb_io_wait_ctxt *wc = bio->bi_private;

	if (bio->bi_status) {
		wc->wc_error = blk_status_to_errno(bio->bi_status);
	}

	o2hb_io_wait_dec(wc, 1);
	bio_put(bio);
}

static void o2hb_async_bio_end_io(struct bio *bio)
{
	struct o2hb_async_write_io_list *w_io = bio->bi_private;
	struct o2hb_region *reg = w_io->reg;
	unsigned long flags;

	if (!bio->bi_status && !atomic_read(&reg->hr_own_slot_populated)) {
		atomic_set(&reg->hr_own_slot_populated, 1);
		wake_up(&o2hb_write_thread_queue);
	}

	if (!bio->bi_status)
		o2hb_arm_write_timeout(reg);

	spin_lock_irqsave(&reg->hr_lock, flags);
	list_del_init(&w_io->list);

	if (list_empty(&reg->hr_inflight_io))
		wake_up(&o2hb_inflight_io_queue);
	spin_unlock_irqrestore(&reg->hr_lock, flags);

	if (w_io->page)
		__free_page(w_io->page);

	kmem_cache_free(o2hb_inflight_io_cache, w_io);

	bio_put(bio);
}

/* Setup a bio to cover I/O against num_slots slots starting at
 * start_slot. */
static struct bio *o2hb_setup_one_bio(struct o2hb_region *reg,
				      struct o2hb_io_wait_ctxt *wc,
				      unsigned int *current_slot,
				      unsigned int max_slots, int op,
				      int op_flags)
{
	int len, current_page;
	unsigned int vec_len, vec_start;
	unsigned int bits = reg->hr_block_bits;
	unsigned int spp = reg->hr_slots_per_page;
	unsigned int cs = *current_slot;
	struct bio *bio;
	struct page *page;

	/* Testing has shown this allocation to take long enough under
	 * GFP_KERNEL that the local node can get fenced. It would be
	 * nicest if we could pre-allocate these bios and avoid this
	 * all together. */
	bio = bio_alloc(GFP_ATOMIC, 256);
	if (!bio) {
		mlog(ML_ERROR, "%s: Could not alloc slots BIO!\n",
				config_item_name(&reg->hr_item));
		bio = ERR_PTR(-ENOMEM);
		goto bail;
	}

	/* Must put everything in 512 byte sectors for the bio... */
	bio->bi_iter.bi_sector = (reg->hr_start_block + cs) << (bits - 9);
	bio_set_dev(bio, reg->hr_bdev);
	bio->bi_private = wc;
	bio->bi_end_io = o2hb_bio_end_io;
	bio_set_op_attrs(bio, op, op_flags);

	vec_start = (cs << bits) % PAGE_SIZE;
	while (cs < max_slots) {
		current_page = cs / spp;
		page = reg->hr_slot_data[current_page];

		vec_len = min(PAGE_SIZE - vec_start,
			      (max_slots-cs) * (PAGE_SIZE/spp));

		mlog(ML_HB_BIO, "page %d, vec_len = %u, vec_start = %u\n",
		     current_page, vec_len, vec_start);

		len = bio_add_page(bio, page, vec_len, vec_start);
		if (len != vec_len)
			break;

		cs += vec_len / (PAGE_SIZE/spp);
		vec_start = 0;
	}

bail:
	*current_slot = cs;
	return bio;
}

/* Setup an async bio to cover I/O against write heatbeat */
static struct bio *o2hb_setup_one_async_bio(struct o2hb_region *reg,
				      struct o2hb_async_write_io_list *w_io,
				      unsigned int current_slot, int op,
				      int op_flags)
{
	unsigned int vec_len, vec_start, len;
	unsigned int bits = reg->hr_block_bits;
	struct bio *bio;

	/* Testing has shown this allocation to take long enough under
	 * GFP_KERNEL that the local node can get fenced. It would be
	 * nicest if we could pre-allocate these bios and avoid this
	 * all together. */
	bio = bio_alloc(GFP_ATOMIC, 1);
	if (!bio) {
		mlog(ML_ERROR, "%s: Could not alloc slots BIO!\n",
				config_item_name(&reg->hr_item));
		bio = ERR_PTR(-ENOMEM);
		goto bail;
	}

	/* Must put everything in 512 byte sectors for the bio... */
	bio->bi_iter.bi_sector = (reg->hr_start_block + current_slot) << (bits - 9);
	bio_set_dev(bio, reg->hr_bdev);
	bio->bi_private = w_io;
	bio->bi_end_io = o2hb_async_bio_end_io;
	bio_set_op_attrs(bio, op, op_flags);

	vec_start = 0;
	vec_len = reg->hr_block_bytes;

	mlog(ML_HB_BIO, "vec_len = %u, vec_start = %u\n",
		vec_len, vec_start);

	len = bio_add_page(bio, w_io->page, vec_len, vec_start);
	if (len != vec_len) {
		mlog(ML_ERROR, "%s: Adding page to bio failed, page %p, "
				"len %u, vec_len %u, vec_start %u, bi_sector %llu\n",
				config_item_name(&reg->hr_item), w_io->page, len,
				vec_len, vec_start, (unsigned long long)bio->bi_iter.bi_sector);
		bio_put(bio);
		bio = ERR_PTR(-EIO);
	}

bail:
	return bio;
}

static int o2hb_read_slots(struct o2hb_region *reg,
			   unsigned int begin_slot,
			   unsigned int max_slots)
{
	unsigned int current_slot = begin_slot;
	int status;
	struct o2hb_io_wait_ctxt wc;
	struct bio *bio;

	o2hb_io_wait_init(&wc);

	while (current_slot < max_slots) {
		bio = o2hb_setup_one_bio(reg, &wc, &current_slot, max_slots,
					 REQ_OP_READ, 0);
		if (IS_ERR(bio)) {
			status = PTR_ERR(bio);
			mlog_errno(status);
			goto bail_and_wait;
		}

		atomic_inc(&wc.wc_num_reqs);
		submit_bio(bio);
	}

	status = 0;

bail_and_wait:
	o2hb_wait_on_io(&wc);
	if (wc.wc_error && !status)
		status = wc.wc_error;

	return status;
}

static int o2hb_issue_node_write(struct o2hb_region *reg,
				 struct o2hb_io_wait_ctxt *write_wc, int node_num)
{
	int status;
	struct bio *bio;

	o2hb_io_wait_init(write_wc);

	bio = o2hb_setup_one_bio(reg, write_wc, &node_num, node_num+1, REQ_OP_WRITE,
				 REQ_SYNC);
	if (IS_ERR(bio)) {
		status = PTR_ERR(bio);
		mlog_errno(status);
		goto bail;
	}

	atomic_inc(&write_wc->wc_num_reqs);
	submit_bio(bio);

	status = 0;
bail:
	return status;
}

static int o2hb_issue_node_write_async(struct o2hb_region *reg,
				  struct o2hb_async_write_io_list *w_io, int node_num)
{
	int status = 0;
	struct bio *bio;
	unsigned long flags;

	bio = o2hb_setup_one_async_bio(reg, w_io, node_num, REQ_OP_WRITE, REQ_SYNC);
	if (IS_ERR(bio)) {
		status = PTR_ERR(bio);
		mlog_errno(status);
		return status;
	}

	w_io->reg = reg;

	spin_lock_irqsave(&reg->hr_lock, flags);
	list_add(&w_io->list, &reg->hr_inflight_io);
	spin_unlock_irqrestore(&reg->hr_lock, flags);

	submit_bio(bio);

	return status;
}

static u32 o2hb_compute_block_crc_le(struct o2hb_region *reg,
				     struct o2hb_disk_heartbeat_block *hb_block)
{
	__le32 old_cksum;
	u32 ret;

	/* We want to compute the block crc with a 0 value in the
	 * hb_cksum field. Save it off here and replace after the
	 * crc. */
	old_cksum = hb_block->hb_cksum;
	hb_block->hb_cksum = 0;

	ret = crc32_le(0, (unsigned char *) hb_block, reg->hr_block_bytes);

	hb_block->hb_cksum = old_cksum;

	return ret;
}

static void o2hb_dump_slot(struct o2hb_disk_heartbeat_block *hb_block)
{
	mlog(ML_ERROR,
	     "Dump slot information: seq = 0x%llx, node = %u, cksum = 0x%x, generation 0x%llx\n",
	     (long long)le64_to_cpu(hb_block->hb_seq),
	     hb_block->hb_node, le32_to_cpu(hb_block->hb_cksum),
	     (long long)le64_to_cpu(hb_block->hb_generation));
}

static int o2hb_verify_crc(struct o2hb_region *reg,
			   struct o2hb_disk_heartbeat_block *hb_block)
{
	u32 read, computed;

	read = le32_to_cpu(hb_block->hb_cksum);
	computed = o2hb_compute_block_crc_le(reg, hb_block);

	return read == computed;
}

/* We want to make sure that nobody is heartbeating on top of us --
 * this will help detect an invalid configuration. */
static void o2hb_check_own_slot(struct o2hb_region *reg)
{
	struct o2hb_disk_slot *slot;
	struct o2hb_disk_heartbeat_block *hb_block;
	char *errstr = NULL;

	slot = reg->hr_slots[o2nm_this_node()];

	hb_block = slot->ds_raw_block;
	if (le64_to_cpu(hb_block->hb_generation) == reg->hr_generation &&
	    le16_to_cpu(hb_block->hb_node) == o2nm_this_node())
		return;

#define ERRSTR1		"Another node is heartbeating on device"
#define ERRSTR2		"Heartbeat generation mismatch on device"

	if (le16_to_cpu(hb_block->hb_node) != o2nm_this_node())
		errstr = ERRSTR1;
	else if (le64_to_cpu(hb_block->hb_generation) !=
		 reg->hr_generation)
		errstr = ERRSTR2;

	mlog(ML_ERROR,
	     "%s (%s): expected(%u:0x%llx), ondisk(%u:0x%llx)\n",
		 errstr, reg->hr_dev_name,
	     o2nm_this_node(), (unsigned long long)reg->hr_generation,
	     le16_to_cpu(hb_block->hb_node),
	     (unsigned long long)le64_to_cpu(hb_block->hb_generation));
}

static inline void o2hb_fill_block(struct o2hb_region *reg,
							u64 generation,
							struct o2hb_disk_heartbeat_block *hb_block)
{
	int node_num;
	u64 cputime;
	struct o2nm_node *tmp_node;
	unsigned long flags;
	int node = -1, i;

	node_num = o2nm_this_node();
	tmp_node = o2nm_get_node_by_num(node_num);
	if (!tmp_node)
		return;

	memset(hb_block, 0, reg->hr_block_bytes);
	for (i = 0; i < MAX_HOSTS; i++)
		hb_block->hr_nodes[i].host_id = cpu_to_le16(O2NM_INVALID_NODE_NUM);

	/* TODO: time stuff */
	cputime = ktime_get_real_seconds();
	if (!cputime)
		cputime = 1;

	hb_block->hb_seq = cpu_to_le64(cputime);
	hb_block->hb_node = cpu_to_le16(node_num);
	hb_block->hb_generation = cpu_to_le64(generation);
	hb_block->hb_dead_ms = cpu_to_le32(o2hb_real_dead_threshold *
			O2HB_REGION_TIMEOUT_MS);
	hb_block->hb_ipnet_type = tmp_node->nd_ipnet_type;
	if (hb_block->hb_ipnet_type == IPV4_TYPE)
		hb_block->ip_addr.hb_ipv4_address =
			cpu_to_le32(be32_to_cpu(tmp_node->nd_ipv4_address));
	else
		memcpy(hb_block->ip_addr.hb_ipv6_address,
			tmp_node->nd_ipv6_address, sizeof(tmp_node->nd_ipv6_address));
	hb_block->hb_lock_type = reg->lock_type_adl;
	/* handle multi network partitions */
	spin_lock_irqsave(&reg->hr_lock, flags);
	hb_block->hb_qs_effect_nodes =
			cpu_to_le16(o2quo_calculate_effect_nodes(reg->hr_live_node_bitmap,
				node_num));
	hb_block->hb_qs_lowest_node =
			cpu_to_le16(o2quo_get_lowest_node(reg->hr_live_node_bitmap, node_num));

	i = 0;
	while ((node = find_next_bit(reg->hr_live_node_bitmap,
		O2NM_MAX_NODES, node + 1)) < O2NM_MAX_NODES) {
		struct o2hb_disk_slot *slot = reg->hr_slots[node];

		if (i >= MAX_HOSTS) {
			mlog(ML_ERROR,
					"%s: current host number(%d) exceed maximum(%d), node = %d\n",
					config_item_name(&reg->hr_item), i, MAX_HOSTS, node);
			break;
		}

		hb_block->hr_nodes[i].host_id = cpu_to_le16(node);
		hb_block->hr_nodes[i].host_generation = cpu_to_le32((u32)slot->ds_last_generation);
		i++;
	}
	spin_unlock_irqrestore(&reg->hr_lock, flags);

	/* This step must always happen last! */
	hb_block->hb_cksum = cpu_to_le32(o2hb_compute_block_crc_le(reg,
								   hb_block));

	mlog(ML_HB_BIO, "our node generation = 0x%llx, cksum = 0x%x, IP = %pI4\n",
	     (long long)generation,
	     le32_to_cpu(hb_block->hb_cksum),
	     &(tmp_node->nd_ipv4_address));

	if (tmp_node)
		o2nm_node_put(tmp_node);
}

static inline void o2hb_prepare_block(struct o2hb_region *reg,
				      u64 generation)
{
	int node_num;
	struct o2hb_disk_slot *slot;

	node_num = o2nm_this_node();
	slot = reg->hr_slots[node_num];
	o2hb_fill_block(reg, generation, slot->ds_raw_block);
}

/*
 * set heartbeat generateion, currently we only used 0x2
 */
static int o2hb_set_hb_generation(char *uuid, u16 node_num, u64 generation)
{
	int ret = 0;
	struct o2hb_region *reg;
	struct o2hb_io_wait_ctxt write_wc;
	struct o2hb_disk_slot *slot = NULL;
	struct o2hb_disk_heartbeat_block *hb_block;

	spin_lock(&o2hb_live_lock);
	list_for_each_entry(reg, &o2hb_all_regions, hr_all_item) {
		if (reg->hr_item_dropped || !config_item_name(&reg->hr_item))
			continue;
		if (!strncmp(uuid, config_item_name(&reg->hr_item),
				strlen(config_item_name(&reg->hr_item)))) {
			slot = reg->hr_slots[node_num];
			if (!list_empty(&slot->ds_live_item)) {
				spin_unlock(&o2hb_live_lock);
				return ret;
			}
			break;
		}
	}
	spin_unlock(&o2hb_live_lock);

	if (slot != NULL) {
		mlog(ML_NOTICE,
				"Domain: %s. Setting heartbeat generation for node %d\n",
				uuid, node_num);
		spin_lock(&slot->ds_lock);
		hb_block = (struct o2hb_disk_heartbeat_block *)slot->ds_raw_block;
		hb_block->hb_generation = cpu_to_le64(generation);
		hb_block->hb_cksum = cpu_to_le32(o2hb_compute_block_crc_le(reg,
				hb_block));
		spin_unlock(&slot->ds_lock);
		ret = o2hb_issue_node_write(reg, &write_wc, node_num);
		if (ret == 0)
			o2hb_wait_on_io(&write_wc);
		else
			mlog_errno(ret);
	}

	return ret;
}

static void o2hb_fire_callbacks(struct o2hb_callback *hbcall,
				struct o2nm_node *node, int idx,
				char *uuid, u64 hb_generation)
{
	struct o2hb_callback_func *f;

	if (uuid == NULL) {
		mlog(ML_ERROR,
			"uuid is NULL when firing callbacks for node %d, return\n", idx);
		return;
	}

	list_for_each_entry(f, &hbcall->list, hc_item) {
		if (f->region_uuid[0] != '\0' &&
				memcmp(uuid, f->region_uuid, strlen(f->region_uuid)) != 0)
			continue;

		mlog(ML_NOTICE, "calling funcs %p, idx %d, uuid %s\n", f, idx, uuid);
		(f->hc_func)(node, idx, f->hc_data, hb_generation);
	}
}

/* Will run the list in order until we process the passed event */
static void o2hb_run_event_list(struct o2hb_node_event *queued_event)
{
	struct o2hb_callback *hbcall;
	struct o2hb_node_event *event;

	/* Holding callback sem assures we don't alter the callback
	 * lists when doing this, and serializes ourselves with other
	 * processes wanting callbacks. */
	down_write(&o2hb_callback_sem);

	spin_lock(&o2hb_live_lock);
	while (!list_empty(&o2hb_node_events)
	       && !list_empty(&queued_event->hn_item)) {
		event = list_entry(o2hb_node_events.next,
				   struct o2hb_node_event,
				   hn_item);
		list_del_init(&event->hn_item);
		spin_unlock(&o2hb_live_lock);

		mlog(ML_NOTICE, "o2hb: Fire event %d(%p) for node %d\n",
		     event->hn_event_type, event, event->hn_node_num);

		hbcall = hbcall_from_type(event->hn_event_type);

		/* We should *never* have gotten on to the list with a
		 * bad type... This isn't something that we should try
		 * to recover from. */
		BUG_ON(IS_ERR(hbcall));

		o2hb_fire_callbacks(hbcall, event->hn_node, event->hn_node_num,
				event->region_uuid, event->hb_generation);
		spin_lock(&o2hb_live_lock);
	}
	spin_unlock(&o2hb_live_lock);

	up_write(&o2hb_callback_sem);
}

static void o2hb_queue_node_event(struct o2hb_node_event *event,
				  enum o2hb_callback_type type,
				  struct o2nm_node *node,
				  int node_num,
				  const char *uuid,
				  u64 hb_generation)
{
	assert_spin_locked(&o2hb_live_lock);

	BUG_ON((!node) && (type != O2HB_NODE_DOWN_CB) &&
			(type != O2HB_REGION_DOWN_CB) &&
			(type != O2HB_FENCE_SCSI_CB));

	event->hn_event_type = type;
	event->hn_node = node;
	event->hn_node_num = node_num;
	event->hb_generation = hb_generation;
	memset(event->region_uuid, 0, O2HB_REGION_UUID_LEN + 1);
	if (uuid)
		memcpy(event->region_uuid, uuid, O2HB_REGION_UUID_LEN);

	mlog(ML_NOTICE, "o2hb: Queue event %d(%p) for node %d\n",
	     type, event, node_num);

	list_add_tail(&event->hn_item, &o2hb_node_events);
}

/*
 * detect current node dlm lock lun existed or not,
 * return: 1 means the dlm lock lun existed, 0 means dlm lock
 * lun not existed.
 */
int o2hb_dlm_existed(u16 node_num)
{
	struct o2hb_disk_slot *temp_slot;

	assert_spin_locked(&o2hb_live_lock);
	list_for_each_entry(temp_slot, &o2hb_live_slots[node_num],
			ds_live_item) {
		if (!temp_slot->reg->lock_type_adl)
			return 1;
	}

	return 0;
}

static void o2hb_shutdown_slot(struct o2hb_disk_slot *slot)
{
	struct o2hb_node_event event = {
		.hn_item = LIST_HEAD_INIT(event.hn_item),
	};
	struct o2nm_node *node;
	struct o2hb_region *reg = slot->reg;
	unsigned long flags;
	int tmp, queued = 0;

	node = o2nm_get_node_by_num(slot->ds_node_num);
	if (!node) {
		spin_lock(&o2hb_live_lock);
		tmp = test_bit(slot->ds_node_num, o2hb_live_node_bitmap);
		spin_unlock(&o2hb_live_lock);
		if (!tmp)
			return;
	}

	spin_lock(&o2hb_live_lock);
	if (!list_empty(&slot->ds_live_item)) {
		mlog(ML_NOTICE, "Shutdown, node %d leaves region %s\n",
			slot->ds_node_num, config_item_name(&slot->reg->hr_item));

		spin_lock_irqsave(&reg->hr_lock, flags);
		reg->hr_qs_effect_nodes[slot->ds_node_num] = 0;
		reg->hr_qs_lowest_node[slot->ds_node_num] = O2NM_MAX_NODES;
		clear_bit(slot->ds_node_num, reg->hr_live_node_bitmap);
		reg->hr_heartbeating--;
		spin_unlock_irqrestore(&reg->hr_lock, flags);

		clear_bit(slot->ds_node_num, reg->hr_inconsist_map);
		list_del_init(&slot->ds_live_item);
		if (!list_empty(&o2hb_live_slots[slot->ds_node_num])) {
			o2hb_queue_node_event(&event, O2HB_REGION_DOWN_CB,
					node, slot->ds_node_num,
					config_item_name(&reg->hr_item),
					slot->ds_last_generation);
		} else {
			clear_bit(slot->ds_node_num, o2hb_live_node_bitmap);
			mlog(ML_NOTICE,
					"Remove node %d from live nodes bitmap, uuid %s\n",
					slot->ds_node_num,
					config_item_name(&reg->hr_item));

			o2hb_queue_node_event(&event, O2HB_NODE_DOWN_CB,
					node, slot->ds_node_num,
					config_item_name(&reg->hr_item),
					slot->ds_last_generation);
		}

		o2quo_hb_down(slot->ds_node_num, reg->lock_type_adl);

		queued = 1;
	}
	spin_unlock(&o2hb_live_lock);

	if (queued)
		o2hb_run_event_list(&event);

	if (node)
		o2nm_node_put(node);
}

static void o2hb_set_quorum_device(struct o2hb_region *reg,
				   struct o2hb_disk_slot *slot)
{
	unsigned long flags;

	assert_spin_locked(&o2hb_live_lock);

	if (!o2hb_global_heartbeat_active())
		return;

	if (test_bit(reg->hr_region_num, o2hb_quorum_region_bitmap))
		return;

	/*
	 * A region can be added to the quorum only when it sees all
	 * live nodes heartbeat on it. In other words, the region has been
	 * added to all nodes.
	 */
	spin_lock_irqsave(&reg->hr_lock, flags);
	if (memcmp(reg->hr_live_node_bitmap, o2hb_live_node_bitmap,
		   sizeof(o2hb_live_node_bitmap))) {
		spin_unlock_irqrestore(&reg->hr_lock, flags);
		return;
	}
	spin_unlock_irqrestore(&reg->hr_lock, flags);

	if (slot->ds_changed_samples < O2HB_LIVE_THRESHOLD)
		return;

	mlog(ML_NOTICE, "Region %s is now a quorum device\n",
	       config_item_name(&reg->hr_item));

	set_bit(reg->hr_region_num, o2hb_quorum_region_bitmap);

	/*
	 * If global heartbeat active, unpin all regions if the
	 * region count > CUT_OFF
	 */
	if (bitmap_weight(o2hb_quorum_region_bitmap,
			   O2NM_MAX_REGIONS) > O2HB_PIN_CUT_OFF)
		o2hb_region_unpin(NULL);
}

static void o2hb_conn_failure_set(struct o2hb_region *reg,
		u16 node_idx, u16 err_node)
{
	unsigned long flags;

	spin_lock_irqsave(&reg->hr_lock, flags);

	if (!test_bit(node_idx, reg->hr_conn_failure_bm[err_node])) {
		reg->hr_conn_failure_votes[err_node]++;
		set_bit(node_idx, reg->hr_conn_failure_bm[err_node]);
	}

	mlog(0, "node: %u, %d total\n", err_node,
			reg->hr_conn_failure_votes[err_node]);

	spin_unlock_irqrestore(&reg->hr_lock, flags);

}

static void o2hb_conn_failure_query(struct work_struct *work)
{
	struct o2hb_disk_slot *slot =
			container_of(work, struct o2hb_disk_slot, conn_failure_query);
	struct o2hb_region *reg = slot->reg;
	u16 err_node = slot->ds_node_num;
	int target = -1, ret, response;
	unsigned long live_nodemap[BITS_TO_LONGS(O2NM_MAX_NODES)];
	unsigned long flags;

	spin_lock_irqsave(&reg->hr_lock, flags);
	memset(reg->hr_conn_failure_bm[err_node], 0,
			sizeof(unsigned long) * BITS_TO_LONGS(O2NM_MAX_NODES));
	reg->hr_conn_failure_votes[err_node] = 0;
	memcpy(live_nodemap, reg->hr_live_node_bitmap,
			sizeof(reg->hr_live_node_bitmap));
	spin_unlock_irqrestore(&reg->hr_lock, flags);

	while (1) {
		target = find_next_bit(live_nodemap, O2NM_MAX_NODES, target + 1);
		if (target >= O2NM_MAX_NODES)
			break;
		if (target == err_node || target == o2nm_this_node())
			continue;

		ret = o2hb_send_one_query(err_node, target, &response);
		if (ret < 0)
			mlog(ML_ERROR,
				"Error %d sending connection query message to node %u for err_node %u\n",
				ret, target, err_node);
		if (!ret && response)
			o2hb_conn_failure_set(reg, target, err_node);
	}

	if (o2quo_conn_check_err(err_node))
		o2hb_conn_failure_set(reg, o2nm_this_node(), err_node);

	spin_lock(&slot->ds_lock);
	slot->conn_query_done = 1;
	spin_unlock(&slot->ds_lock);
}

static int o2hb_check_conn_down(struct o2hb_region *reg,
		struct o2hb_disk_slot *slot)
{
	int query_done, send_query = 0, conn_down = 0;

	if (!slot->ds_fake_timeout) {
		slot->ds_fake_timeout = 1;
		send_query = 1;
		goto conn_query;
	}

	spin_lock(&slot->ds_lock);
	query_done = slot->conn_query_done;
	spin_unlock(&slot->ds_lock);

	if (query_done)
		conn_down = o2hb_conn_down(reg, slot->ds_node_num);

	if (query_done && !conn_down)
		send_query = 1;

conn_query:
	if (send_query) {
		spin_lock(&slot->ds_lock);
		slot->conn_query_done = 0;
		spin_unlock(&slot->ds_lock);

		queue_work(reg->o2hb_worker, &slot->conn_failure_query);
	}

	return conn_down;
}

void o2hb_disk_timeout(char *uuid)
{
	struct o2hb_region *reg;

	spin_lock(&o2hb_live_lock);
	list_for_each_entry(reg, &o2hb_all_regions, hr_all_item) {
		if (reg->hr_item_dropped || !config_item_name(&reg->hr_item))
			continue;
		if (!strncmp(uuid, config_item_name(&reg->hr_item),
				strlen(config_item_name(&reg->hr_item)))) {
			mlog(ML_NOTICE, "%s, disk timeout, setting unclean stop!\n", uuid);
			reg->hr_unclean_stop = 1;
			break;
		}
	}
	spin_unlock(&o2hb_live_lock);
}
EXPORT_SYMBOL_GPL(o2hb_disk_timeout);

/* local heartbeat mode, multi volume mounted on one node,
 * we make decision based on region when network partition occurs */
void o2hb_quorum_region(void)
{
	struct o2hb_region *reg;
	unsigned long flags;
	int node_num;

	node_num = o2nm_this_node();
	spin_lock(&o2hb_live_lock);
	list_for_each_entry(reg, &o2hb_all_regions, hr_all_item) {
		if (reg->hr_item_dropped || !config_item_name(&reg->hr_item))
			continue;
		o2hb_node_state_change(reg, node_num, NODE_STATE_QUORUM);

		if (reg->hr_unclean_stop) {
			mlog(ML_NOTICE, "%s: Skip quorum because of disk timeout\n",
					config_item_name(&reg->hr_item));
			continue;
		}

		if (reg->lock_type_adl) {
			mlog(ML_NOTICE, "%s: Skip quorum because of this is adl region\n",
					config_item_name(&reg->hr_item));
			continue;
		}

		spin_lock_irqsave(&reg->hr_lock, flags);
		if (o2quo_make_decision(reg->hr_live_node_bitmap, reg->hr_heartbeating,
				reg->hr_qs_effect_nodes, reg->hr_qs_lowest_node)) {
			reg->hr_fence = 1;
			spin_unlock_irqrestore(&reg->hr_lock, flags);
			spin_unlock(&o2hb_live_lock);
			o2hb_node_state_change(reg, node_num, NODE_STATE_FENCE);
			if (o2nm_fence_method_is_invalid()) {
				printk(KERN_WARNING
					"%s: This region should fence self by setting invalid!\n",
					config_item_name(&reg->hr_item));
				fence_invalid = 1;
				o2hb_handle_invalid(config_item_name(&reg->hr_item));
				spin_lock(&o2hb_live_lock);
				continue;
			} else {
				o2quo_disk_timeout();
				return;
			}
		}
		spin_unlock_irqrestore(&reg->hr_lock, flags);
	}
	spin_unlock(&o2hb_live_lock);
}

static int o2hb_send_query(struct o2hb_region *reg, u16 target_node,
		u16 node_num, int *response)
{
	struct o2hb_query_join query_msg;

	memset(&query_msg, 0, sizeof(query_msg));
	query_msg.node_num = cpu_to_be16(node_num);
	/* send my generation to those already in domain */
	query_msg.genaration =
		cpu_to_be64((reg->hr_slots[node_num])->ds_last_generation);
	memcpy(query_msg.uuid_name, config_item_name(&reg->hr_item),
			strlen(config_item_name(&reg->hr_item)));

	return o2net_pre_send_message(&query_msg, sizeof(query_msg),
				target_node, response);
}

/* check whether this node is in region */
static void o2hb_pre_check_node(struct work_struct *work)
{
	struct o2hb_disk_slot *slot =
			container_of(work, struct o2hb_disk_slot, ds_pre_check_node);
	struct o2hb_region *reg = slot->reg;
	struct o2hb_node_event event = {
		.hn_item = LIST_HEAD_INIT(event.hn_item),
	};
	int ret = 0, response = 0, queued = 0;
	unsigned long flags;

	if (!test_bit(slot->ds_node_num, reg->hr_timeout_map)) {
		mlog(ML_NOTICE, "node %d has already been checked, region: %s.\n",
				slot->ds_node_num, config_item_name(&reg->hr_item));
		return;
	}

	if (slot->ds_equal_samples >= o2hb_real_dead_threshold) {
		mlog(ML_NOTICE,
			"node %d doesn't have heartbeat in region : %s, so I will set generation.\n",
			slot->ds_node_num,
			config_item_name(&reg->hr_item));
		goto set_generation;
	}

	ret = o2net_pre_start_connect(slot->ds_node_num);
	if (ret < 0) {
		if (slot->ds_equal_samples < o2hb_real_dead_threshold)
			return;
		goto set_generation;
	}

	ret = o2hb_send_query(reg, slot->ds_node_num, o2nm_this_node(), &response);
	if (ret < 0) {
		o2net_pre_shutdown_socket(slot->ds_node_num);
		/* when slot->ds_equal_samples == o2hb_real_dead_threshold, we should
		 * think node is not in region */
		if (slot->ds_equal_samples < o2hb_real_dead_threshold)
			return;
		goto set_generation;
	}

	if (!response) {
		mlog(ML_NOTICE, "node %d isn't in region : %s\n", slot->ds_node_num,
				config_item_name(&reg->hr_item));
		o2net_pre_shutdown_socket(slot->ds_node_num);
		goto out;
	} else {
		struct o2nm_node *node = o2nm_get_node_by_num(slot->ds_node_num);

		if (!node)
			goto out;

		spin_lock(&o2hb_live_lock);
		if (list_empty(&slot->ds_live_item)) {
			mlog(ML_NOTICE, "Node %d (id 0x%llx) joined my region %s\n",
					slot->ds_node_num, (long long)slot->ds_last_generation,
					config_item_name(&reg->hr_item));

			spin_lock_irqsave(&reg->hr_lock, flags);
			set_bit(slot->ds_node_num, reg->hr_live_node_bitmap);
			reg->hr_heartbeating++;
			spin_unlock_irqrestore(&reg->hr_lock, flags);

			if (!list_empty(&o2hb_live_slots[slot->ds_node_num])) {
				o2hb_queue_node_event(&event, O2HB_REGION_UP_CB,
						node, slot->ds_node_num,
						config_item_name(&reg->hr_item),
						slot->ds_last_generation);
			} else {
				/* first on the list generates a callback */
				mlog(ML_NOTICE,
						"Add node %d to live nodes bitmap, uuid %s\n",
						slot->ds_node_num,
						config_item_name(&reg->hr_item));
				set_bit(slot->ds_node_num, o2hb_live_node_bitmap);

				o2hb_queue_node_event(&event, O2HB_NODE_UP_CB, node,
						slot->ds_node_num,
						config_item_name(&reg->hr_item),
						slot->ds_last_generation);
			}

			o2quo_hb_up(slot->ds_node_num, reg->lock_type_adl);

			queued = 1;
			list_add_tail(&slot->ds_live_item,
					&o2hb_live_slots[slot->ds_node_num]);

			o2hb_node_state_change(reg, slot->ds_node_num, NODE_STATE_MOUNTED);
		}
		spin_unlock(&o2hb_live_lock);
		o2nm_node_put(node);
		if (queued)
			o2hb_run_event_list(&event);
		o2net_pre_shutdown_socket(slot->ds_node_num);
		goto out;
	}

set_generation:
	ret = o2hb_set_hb_generation(config_item_name(&reg->hr_item),
			slot->ds_node_num, 2);

out:
	spin_lock(&slot->ds_lock);
	slot->ds_pre_checked = 1;
	clear_bit(slot->ds_node_num, reg->hr_timeout_map);
	spin_unlock(&slot->ds_lock);

	return;
}

/* Before making quorum, we check if disk heartbeat is already stable */
int o2hb_check_if_quorum(void)
{
	struct o2hb_region *reg;
	unsigned long flags;
	int i;

	spin_lock(&o2hb_live_lock);
	/* when node begin make quorum, set this flag */
	list_for_each_entry(reg, &o2hb_all_regions, hr_all_item) {
		if (reg->hr_item_dropped || !config_item_name(&reg->hr_item))
			continue;

		if (reg->hr_unclean_stop)
			continue;

		spin_lock_irqsave(&reg->hr_lock, flags);
		/* check if all nodes have a stable state */
		for (i = 0; i < O2NM_MAX_NODES; i++) {
			if (test_bit(i, reg->hr_live_node_bitmap) &&
				test_bit(i, reg->hr_quorum_bitmap)) {
				spin_unlock_irqrestore(&reg->hr_lock, flags);
				spin_unlock(&o2hb_live_lock);
				return 0;
			}
		}
		spin_unlock_irqrestore(&reg->hr_lock, flags);
	}
	spin_unlock(&o2hb_live_lock);

	return 1;
}

static void o2hb_set_quorum_bitmap(struct o2hb_region *reg,
		struct o2hb_disk_slot *slot)
{
	unsigned long flags;

	spin_lock_irqsave(&reg->hr_lock, flags);
	if (slot->ds_equal_samples == 0 ||
			slot->ds_equal_samples >= o2hb_real_dead_threshold)
		clear_bit(slot->ds_node_num, reg->hr_quorum_bitmap);
	else
		set_bit(slot->ds_node_num, reg->hr_quorum_bitmap);
	spin_unlock_irqrestore(&reg->hr_lock, flags);
}

static int o2hb_check_consistency(struct o2hb_region *reg,
		struct o2hb_disk_heartbeat_block *hb_block)
{
	int i;
	__be32 ipv4_address = 0;
	__u8 ipv6_address[16] = {0};

	for (i = 0; i < MAX_HOSTS; i++) {
		if (le16_to_cpu(hb_block->hr_nodes[i].host_id) == o2nm_this_node()) {
			if (le32_to_cpu(hb_block->hr_nodes[i].host_generation) ==
					(u32)reg->hr_generation) {
				return 1;
			} else {
				if (hb_block->hb_ipnet_type == IPV4_TYPE) {
					ipv4_address = cpu_to_be32(le32_to_cpu(
						hb_block->ip_addr.hb_ipv4_address));
					mlog(ML_NOTICE,
						"node %d (ip: %pI4) had catched my heartbeating, "
						"but generation mismatch. generation 0x%x, mine 0x%llx\n",
						le16_to_cpu(hb_block->hb_node),
						&ipv4_address,
						le32_to_cpu(hb_block->hr_nodes[i].host_generation),
						(long long)reg->hr_generation);
				} else {
					memcpy(ipv6_address, hb_block->ip_addr.hb_ipv6_address,
						sizeof(ipv6_address));
					mlog(ML_NOTICE,
						"node %d (ip: %pI6) had catched my heartbeating, "
						"but generation mismatch. generation 0x%x, mine 0x%llx\n",
						le16_to_cpu(hb_block->hb_node),
						ipv6_address,
						le32_to_cpu(hb_block->hr_nodes[i].host_generation),
						(long long)reg->hr_generation);
				}
				return 0;
			}
		}
	}

	if (hb_block->hb_ipnet_type == IPV4_TYPE) {
		ipv4_address = cpu_to_be32(le32_to_cpu(hb_block->ip_addr.hb_ipv4_address));
		mlog(ML_NOTICE, "node %d (ip: %pI4) had not catch my heartbeating\n",
				le16_to_cpu(hb_block->hb_node),
				&ipv4_address);
	} else {
		memcpy(ipv6_address, hb_block->ip_addr.hb_ipv6_address, sizeof(ipv6_address));
		mlog(ML_NOTICE, "node %d (ip: %pI6) had not catch my heartbeating\n",
				le16_to_cpu(hb_block->hb_node),
				ipv6_address);
	}
	return 0;
}

#define BUF_LEN			255
#define CMD_LEN			16
#define PROUT_PREEMPT_ABORT	0x5
#define LU_SCOPE		0x0 /* PR applies to the full logical unit */
#define W_EXCLUSIVE_ONLY	0x5 /* Write Exclusive Registrants Only */
#define PR_FIXED_LEN		0x18 /* parameter list len */

static inline int get_fence_scsi_key(int node_num)
{
	return (node_num == 0) ? O2NM_MAX_NODES : node_num;
}

static void o2hb_setup_pa_cmd(unsigned char *cmd, unsigned char *buf,
		int node_num)
{
	int reservation_key = get_fence_scsi_key(o2nm_this_node());
	int service_reservation_key = get_fence_scsi_key(node_num);

	/* parameter list */
	memset(buf, 0, BUF_LEN);
	put_unaligned_be64(reservation_key, &buf[0]);
	put_unaligned_be64(service_reservation_key, &buf[8]);

	/* PERSISTENT RESERVE OUT CDB */
	memset(cmd, 0, CMD_LEN);
	cmd[0] = PERSISTENT_RESERVE_OUT; /* operation code */
	cmd[1] = PROUT_PREEMPT_ABORT; /* service action code */
	cmd[2] = (W_EXCLUSIVE_ONLY & 0x0f) | (LU_SCOPE & 0xf0);
	put_unaligned_be32(PR_FIXED_LEN, &cmd[5]); /* parameter list len */
}

void o2hb_end_req(struct request *rq, blk_status_t error)
{
	struct o2hb_io_wait_ctxt *rb = rq->end_io_data;
	int result = scsi_req(rq)->result;

	if (result || error)
		rb->wc_error = result ? result : blk_status_to_errno(error);

	o2hb_io_wait_dec(rb, 1);

	rq->end_io_data = NULL;
	blk_put_request(rq);
}
EXPORT_SYMBOL_GPL(o2hb_end_req);

int o2hb_execute_req(struct block_device *bdev, int rw,
		char *buf, unsigned int buf_len, unsigned char *cmd,
		struct o2hb_io_wait_ctxt *rb, int nowait)
{
	int ret;
	struct request *rq;
	struct request_queue *q;

	if (!bdev->bd_disk) {
		ret = -EINVAL;
		mlog_errno(ret);
		return ret;
	}

	q = bdev->bd_disk->queue;
	if (!q) {
		ret = -EINVAL;
		mlog_errno(ret);
		return ret;
	}

	if (buf_len > (queue_max_hw_sectors(q) << 9) || !buf_len)
		return -EINVAL;

	rq = blk_get_request(q, rw, 0);
	if (IS_ERR(rq)) {
		ret = PTR_ERR(rq);
		mlog_errno(ret);
		return ret;
	}

	scsi_req(rq)->cmd_len = COMMAND_SIZE(cmd[0]);
	memcpy(scsi_req(rq)->cmd, cmd, scsi_req(rq)->cmd_len);
	rq->__sector = 0;
	rq->timeout = START_STOP_TIMEOUT;
	scsi_req(rq)->retries = 0;

	ret = blk_rq_map_kern(q, rq, buf, buf_len, GFP_NOFS);
	if (ret) {
		mlog_errno(ret);
		blk_put_request(rq);
		return ret;
	}

	if (nowait) {
		if (rb) {
			atomic_inc(&rb->wc_num_reqs);
			rq->end_io_data = rb;
			blk_execute_rq_nowait(q, bdev->bd_disk, rq, 0, o2hb_end_req);
		} else {
			blk_execute_rq_nowait(q, bdev->bd_disk, rq, 0, NULL);
		}
	} else {
		blk_execute_rq(q, bdev->bd_disk, rq, 0);
		ret = scsi_req(rq)->result ? -EIO : 0;
		blk_put_request(rq);
	}

	return ret;
}
EXPORT_SYMBOL_GPL(o2hb_execute_req);

void o2hb_free_device_list(struct list_head *dev_list)
{
	struct dev_segment *dev_seg = NULL, *tmp;

	list_for_each_entry_safe(dev_seg, tmp, dev_list, list) {
		list_del_init(&dev_seg->list);
		kfree(dev_seg);
	}
}
EXPORT_SYMBOL_GPL(o2hb_free_device_list);

int o2hb_get_dev_segment(struct block_device *bdev,
		struct list_head *dev_list)
{
	struct dev_segment *dev_seg = NULL;

	dev_seg = kzalloc(sizeof(struct dev_segment), GFP_KERNEL);
	if (!dev_seg) {
		mlog(ML_ERROR, "alloc dev segment failed.\n");
		return -ENOMEM;
	}

	dev_seg->start = 0;
	dev_seg->len = i_size_read(bdev->bd_inode);
	dev_seg->dvd = bdev->bd_dev;
	memcpy(dev_seg->disk_name, bdev->bd_disk->disk_name,
			sizeof(bdev->bd_disk->disk_name));
	list_add_tail(&dev_seg->list, dev_list);
	return 0;
}
EXPORT_SYMBOL_GPL(o2hb_get_dev_segment);

void o2hb_fence_scsi(struct block_device *bdev, int node_num)
{
	int ret;
	struct list_head dev_list;
	struct list_head scsi_dev_list;
	unsigned char buf[BUF_LEN] = {0};
	unsigned char cmd[CMD_LEN] = {0};
	struct block_device *tmp_bdev;
	struct dev_segment *dev_seg = NULL;
	struct dev_segment *scsi_dev_seg = NULL;

	INIT_LIST_HEAD(&dev_list);
	ret = dm_get_device_segment(bdev->bd_dev, &dev_list);
	if (ret == -ENOENT) {
		/* ocfs2 volume not a multi device */
		ret = o2hb_get_dev_segment(bdev, &dev_list);
		if (ret < 0) {
			mlog(ML_ERROR, "get dev segment failed, err: %d.\n", ret);
			return;
		}
	}

	if (list_empty(&dev_list)) {
		mlog(ML_ERROR, "dev segment is NULL.\n");
		goto out;
	}

	list_for_each_entry(dev_seg, &dev_list, list) {
		o2hb_setup_pa_cmd(cmd, buf, node_num);

		INIT_LIST_HEAD(&scsi_dev_list);
		ret = dm_get_device_segment(dev_seg->dvd, &scsi_dev_list);
		if (ret == -ENOENT) {
			/* map from multilun device to scsi device */
			scsi_dev_seg = dev_seg;
		} else if (ret) {
			mlog_errno(ret);
			goto out;
		} else {
			/*
			 * first, map from multilun device to multipath device.
			 * then, map from multipath device to scsi device.
			 */
			scsi_dev_seg = list_first_entry(&scsi_dev_list, typeof(*dev_seg), list);
			if (scsi_dev_seg == NULL) {
				ret = -EINVAL;
				mlog_errno(ret);
				goto out;
			}
		}

		/* here, the scsi_dev_seg should be scsi device. */
		if (!strncmp(scsi_dev_seg->disk_name, "dm", strlen("dm"))) {
			if (scsi_dev_seg != dev_seg)
				o2hb_free_device_list(&scsi_dev_list);

			ret = -EINVAL;
			mlog(ML_ERROR, "the device name is %s, it should not be dm device\n",
					scsi_dev_seg->disk_name);
			goto out;
		}

		tmp_bdev = blkdev_get_by_dev(scsi_dev_seg->dvd, FMODE_WRITE | FMODE_READ, NULL);
		if (IS_ERR(tmp_bdev)) {
			if (scsi_dev_seg != dev_seg)
				o2hb_free_device_list(&scsi_dev_list);

			mlog(ML_ERROR, "get bdev failed.\n");
			goto out;
		}

		ret = o2hb_execute_req(tmp_bdev, REQ_OP_SCSI_OUT, buf,
				PR_FIXED_LEN, cmd, NULL, 1);
		blkdev_put(tmp_bdev, FMODE_READ|FMODE_WRITE);

		if (scsi_dev_seg != dev_seg)
			o2hb_free_device_list(&scsi_dev_list);

		if (ret < 0) {
			mlog(ML_ERROR, "fence node %d failed\n",
				 node_num);
			goto out;
		}
	}

out:
	o2hb_free_device_list(&dev_list);
}
EXPORT_SYMBOL_GPL(o2hb_fence_scsi);

static int o2hb_check_slot(struct o2hb_region *reg,
			   struct o2hb_disk_slot *slot)
{
	int changed = 0, gen_changed = 0, node_down = 0;
	struct o2hb_node_event event = {
		.hn_item = LIST_HEAD_INIT(event.hn_item),
	};
	struct o2hb_node_event fence_event = {
		.hn_item = LIST_HEAD_INIT(event.hn_item),
	};
	struct o2nm_node *node;
	struct o2hb_disk_heartbeat_block *hb_block = reg->hr_tmp_block;
	u64 cputime;
	unsigned int dead_ms = o2hb_real_dead_threshold *
			O2HB_REGION_TIMEOUT_MS;
	unsigned int slot_dead_ms;
	int tmp = 0;
	int queued = 0;
	unsigned long flags;

	memcpy(hb_block, slot->ds_raw_block, reg->hr_block_bytes);

	/*
	 * If a node is no longer configured but is still in the livemap, we
	 * may need to clear that bit from the livemap.
	 */
	node = o2nm_get_node_by_num(slot->ds_node_num);
	if (!node) {
		spin_lock(&o2hb_live_lock);
		tmp = test_bit(slot->ds_node_num, o2hb_live_node_bitmap);
		spin_unlock(&o2hb_live_lock);
		if (!tmp)
			return 0;
	}

	if (!o2hb_verify_crc(reg, hb_block)) {
		/* all paths from here will drop o2hb_live_lock for
		 * us. */
		spin_lock(&o2hb_live_lock);

		/* Don't print an error on the console in this case -
		 * a freshly formatted heartbeat area will not have a
		 * crc set on it. */
		if (list_empty(&slot->ds_live_item))
			goto out;

		/* The node is live but pushed out a bad crc. We
		 * consider it a transient miss but don't populate any
		 * other values as they may be junk. */
		mlog(ML_ERROR, "%s: Node %d has written a bad crc to %s\n",
		     config_item_name(&reg->hr_item),
		     slot->ds_node_num, reg->hr_dev_name);
		o2hb_dump_slot(hb_block);

		slot->ds_equal_samples++;
		goto fire_callbacks;
	}

	/* we don't care if these wrap.. the state transitions below
	 * clear at the right places */
	cputime = le64_to_cpu(hb_block->hb_seq);
	if (slot->ds_last_time != cputime) {
		slot->ds_changed_samples++;
		slot->ds_fake_timeout = 0;

		spin_lock(&o2hb_live_lock);
		if (!list_empty(&slot->ds_live_item) &&
				test_bit(slot->ds_node_num, reg->hr_inconsist_map) &&
				o2hb_check_consistency(reg, hb_block))
			clear_bit(slot->ds_node_num, reg->hr_inconsist_map);
		spin_unlock(&o2hb_live_lock);

		spin_lock_irqsave(&reg->hr_lock, flags);
		reg->hr_qs_effect_nodes[slot->ds_node_num] =
				le16_to_cpu(hb_block->hb_qs_effect_nodes);
		reg->hr_qs_lowest_node[slot->ds_node_num] =
				le16_to_cpu(hb_block->hb_qs_lowest_node);
		spin_unlock_irqrestore(&reg->hr_lock, flags);
	} else {
		slot->ds_equal_samples++;
	}
	slot->ds_last_time = cputime;

	spin_lock(&slot->ds_lock);
	if (slot->ds_generation_changed) {
		mlog(ML_NOTICE,
				"%s: Node %d may be changed generation (0x%llx to 0x%llx), skip it\n",
				config_item_name(&reg->hr_item),
				slot->ds_node_num,
				(long long)le64_to_cpu(hb_block->hb_generation),
				(long long)slot->ds_last_generation);
		slot->ds_generation_changed = 0;
		goto skip_change_generation;
	}
	/* The node changed heartbeat generations. We assume this to
	 * mean it dropped off but came back before we timed out. We
	 * want to consider it down for the time being but don't want
	 * to lose any changed_samples state we might build up to
	 * considering it live again. */
	if ((slot->ds_last_generation != le64_to_cpu(hb_block->hb_generation) &&
			le64_to_cpu(hb_block->hb_generation) != 1 &&
			le64_to_cpu(hb_block->hb_generation) != 2) ||
			slot->ds_quorum_fence_samples == DOWN_QUORUM_FENCE_SAMPLES) {
		gen_changed = 1;
		slot->ds_equal_samples = 0;
		if (slot->ds_quorum_fence_samples == DOWN_QUORUM_FENCE_SAMPLES)
			slot->ds_quorum_fence_samples = INVALID_QUORUM_FENCE_SAMPLES;
		mlog(ML_NOTICE,
			"%s: Node %d changed generation (0x%llx to 0x%llx)\n",
			config_item_name(&reg->hr_item),
			slot->ds_node_num,
			(long long)slot->ds_last_generation,
			(long long)le64_to_cpu(hb_block->hb_generation));
	} else if (le64_to_cpu(hb_block->hb_generation) == 1 &&
			slot->ds_quorum_fence_samples != INVALID_QUORUM_FENCE_SAMPLES) {
		slot->ds_quorum_fence_samples++;
	}

	if (slot->ds_last_generation != le64_to_cpu(hb_block->hb_generation) &&
			le64_to_cpu(hb_block->hb_generation) == 2) {
		/* use skip_change_geneartion, because other node set hb_generation
		 * we don't care it, we only care this node umount or remount */
		goto skip_change_generation;
	}

	slot->ds_last_generation = le64_to_cpu(hb_block->hb_generation);

skip_change_generation:
	spin_unlock(&slot->ds_lock);

	mlog(ML_HEARTBEAT, "Slot %d gen 0x%llx cksum 0x%x "
	     "seq %llu last %llu changed %u equal %u quorum fence sample %u\n",
	     slot->ds_node_num, (long long)slot->ds_last_generation,
	     le32_to_cpu(hb_block->hb_cksum),
	     (unsigned long long)le64_to_cpu(hb_block->hb_seq),
	     (unsigned long long)slot->ds_last_time, slot->ds_changed_samples,
	     slot->ds_equal_samples,
	     slot->ds_quorum_fence_samples);

	spin_lock(&o2hb_live_lock);

	/* set quorum bitmap */
	o2hb_set_quorum_bitmap(reg, slot);

fire_callbacks:
	/* dead nodes only come to life after some number of
	 * changes at any time during their dead time */
	if (list_empty(&slot->ds_live_item) &&
			slot->ds_changed_samples >= O2HB_LIVE_THRESHOLD &&
			slot->ds_last_generation != 0 &&
			slot->ds_last_generation != 1 &&
			slot->ds_last_generation != 2) {
		/* The panic problem of preventing nodes from being deleted in the process of
		 * multi Lun simultaneous mounting.
		 * If the node is deleted before join the heartbeat region, do nothing,
		 * o2hb_check_slot_multicluster will handle this.
		 * tmp: Node A removes node B from the cluster, but,
		 * o2hb_live_node_bitmap has been set */
		if (tmp) {
			mlog(ML_ERROR, "%s: Node %d is deleted during mounting\n",
			     config_item_name(&reg->hr_item), slot->ds_node_num);
			goto out;
		}

		mlog(ML_NOTICE, "Node %d (id 0x%llx) joined my region %s\n",
		     slot->ds_node_num, (long long)slot->ds_last_generation,
		     config_item_name(&reg->hr_item));

		clear_bit(slot->ds_node_num, reg->hr_timeout_map);

		spin_lock_irqsave(&reg->hr_lock, flags);
		set_bit(slot->ds_node_num, reg->hr_live_node_bitmap);
		reg->hr_heartbeating++;
		spin_unlock_irqrestore(&reg->hr_lock, flags);

		if (reg->lock_type_adl &&
				slot->ds_node_num != o2nm_this_node() &&
				!o2hb_check_consistency(reg, hb_block))
			set_bit(slot->ds_node_num, reg->hr_inconsist_map);

		if (!list_empty(&o2hb_live_slots[slot->ds_node_num])) {
			o2hb_queue_node_event(&event, O2HB_REGION_UP_CB,
					node, slot->ds_node_num,
					config_item_name(&reg->hr_item),
					slot->ds_last_generation);
		} else {
			/* first on the list generates a callback */
			mlog(ML_NOTICE,
				"Add node %d to live nodes bitmap, uuid %s\n",
				slot->ds_node_num, config_item_name(&reg->hr_item));
			set_bit(slot->ds_node_num, o2hb_live_node_bitmap);

			o2hb_queue_node_event(&event, O2HB_NODE_UP_CB, node,
					slot->ds_node_num,
					config_item_name(&reg->hr_item),
					slot->ds_last_generation);
			changed = 1;
		}

		o2quo_hb_up(slot->ds_node_num, reg->lock_type_adl);

		queued = 1;
		list_add_tail(&slot->ds_live_item,
			      &o2hb_live_slots[slot->ds_node_num]);

		slot->ds_equal_samples = 0;
		slot->ds_fake_timeout = 0;
		slot->ds_quorum_fence_samples = 0;

		o2hb_node_state_change(reg, slot->ds_node_num, NODE_STATE_MOUNTED);

		/* We want to be sure that all nodes agree on the
		 * number of milliseconds before a node will be
		 * considered dead. The self-fencing timeout is
		 * computed from this value, and a discrepancy might
		 * result in heartbeat calling a node dead when it
		 * hasn't self-fenced yet. */
		slot_dead_ms = le32_to_cpu(hb_block->hb_dead_ms);
		if (slot_dead_ms && slot_dead_ms != dead_ms) {
			/* TODO: Perhaps we can fail the region here. */
			mlog(ML_ERROR, "%s: Node %d on device %s has a dead count "
			     "of %u ms, but our count is %u ms.\n"
			     "Please double check your configuration values "
			     "for 'O2HB_REAL_DEAD_THRESHOLD'\n",
			     config_item_name(&reg->hr_item),
			     slot->ds_node_num, reg->hr_dev_name, slot_dead_ms,
			     dead_ms);
		}

		goto out;
	}

	/* nodes have generation but no heartbeat, we must check it by pre_net */
	if (list_empty(&slot->ds_live_item) &&
			slot->ds_last_generation != 0 &&
			slot->ds_last_generation != 1 &&
			slot->ds_last_generation != 2 &&
			atomic_read(&reg->hr_steady_iterations) == 1 &&
			slot->ds_equal_samples <= o2hb_real_dead_threshold) {

		spin_lock(&slot->ds_lock);
		if (slot->ds_pre_checked) {
			spin_unlock(&slot->ds_lock);
			goto out;
		}
		set_bit(slot->ds_node_num, reg->hr_timeout_map);
		spin_unlock(&slot->ds_lock);

		queue_work(reg->o2hb_worker, &slot->ds_pre_check_node);
		goto out;
	}

	/* if the list is dead, we're done.. */
	if (list_empty(&slot->ds_live_item))
		goto out;

	o2hb_disk_state_change(reg, slot);

	/* the disk heartbeat is real timeout */
	if (slot->ds_equal_samples >= o2hb_real_dead_threshold &&
		(slot->ds_node_num != o2nm_this_node())) {
		node_down = 1;
		/* FENCE SCSI NODE */
		o2hb_queue_node_event(&fence_event, O2HB_FENCE_SCSI_CB,
				node, slot->ds_node_num, config_item_name(&reg->hr_item),
				slot->ds_last_generation);
		goto handle_node_down;
	}

	/* generation is changed */
	if (gen_changed && (slot->ds_node_num != o2nm_this_node())) {
		node_down = 1;
		/*
		 * check node if umount or fence which caused
		 * by network err, fence scsi this node
		 */
		if ((slot->ds_last_generation == 0) ||
				(slot->ds_last_generation == 1)) {
			o2hb_queue_node_event(&fence_event, O2HB_FENCE_SCSI_CB,
					node, slot->ds_node_num, config_item_name(&reg->hr_item),
					slot->ds_last_generation);
		}
		goto handle_node_down;
	}

	/* Disk heartbeat is fake timeout and this node is not connected with
	 * any other nodes */
	if (slot->ds_equal_samples >= o2hb_fake_dead_threshold &&
			o2quo_conn_check_err(slot->ds_node_num) &&
			o2hb_check_conn_down(reg, slot) &&
			(slot->ds_node_num != o2nm_this_node()))
		node_down = 1;

handle_node_down:
	if (node_down) {
		if (gen_changed && slot->ds_quorum_fence_samples != INVALID_QUORUM_FENCE_SAMPLES)
			o2hb_node_state_change(reg, slot->ds_node_num, NODE_STATE_UMOUNT);
		else
			o2hb_node_state_change(reg, slot->ds_node_num, NODE_STATE_FENCE);

		spin_lock_irqsave(&reg->hr_lock, flags);
		reg->hr_qs_effect_nodes[slot->ds_node_num] = 0;
		reg->hr_qs_lowest_node[slot->ds_node_num] = O2NM_MAX_NODES;
		clear_bit(slot->ds_node_num, reg->hr_live_node_bitmap);
		reg->hr_heartbeating--;
		spin_unlock_irqrestore(&reg->hr_lock, flags);

		clear_bit(slot->ds_node_num, reg->hr_inconsist_map);

		spin_lock(&slot->ds_lock);
		slot->ds_fake_timeout = 0;
		slot->ds_pre_checked = 0;
		slot->ds_generation_changed = 0;
		spin_unlock(&slot->ds_lock);
		list_del_init(&slot->ds_live_item);
		if (!list_empty(&o2hb_live_slots[slot->ds_node_num])) {
			o2hb_queue_node_event(&event, O2HB_REGION_DOWN_CB,
					node, slot->ds_node_num,
					config_item_name(&reg->hr_item),
					slot->ds_last_generation);
		} else {
			mlog(ML_NOTICE,
				"Remove node %d from live nodes bitmap, uuid %s\n",
				slot->ds_node_num, config_item_name(&reg->hr_item));
			clear_bit(slot->ds_node_num, o2hb_live_node_bitmap);

			/* node can be null */
			o2hb_queue_node_event(&event, O2HB_NODE_DOWN_CB,
					node, slot->ds_node_num,
					config_item_name(&reg->hr_item),
					slot->ds_last_generation);
			changed = 1;
		}

		o2quo_hb_down(slot->ds_node_num, reg->lock_type_adl);

		queued = 1;
		mlog(ML_NOTICE, "Node %d left my region %s\n", slot->ds_node_num,
				config_item_name(&reg->hr_item));
		/* We don't clear this because the node is still
		 * actually writing new blocks. */
		if (!gen_changed)
			slot->ds_changed_samples = 0;
		goto out;
	}

	if (slot->ds_changed_samples) {
		slot->ds_changed_samples = 0;
		slot->ds_equal_samples = 0;
	}
out:
	o2hb_set_quorum_device(reg, slot);

	spin_unlock(&o2hb_live_lock);
	if (queued)
		o2hb_run_event_list(&event);

	if (node)
		o2nm_node_put(node);
	return changed;
}

/* detect if multi clusters share the same volume */
static int o2hb_check_slot_multicluster(struct o2hb_region *reg,
		struct o2hb_disk_slot *slot, unsigned long *configure_map)
{
	struct o2nm_node *tmp_node, *my_node;
	struct o2hb_disk_heartbeat_block *hb_block = reg->hr_tmp_block;
	int ret = 0;
	u64 cputime;

	memcpy(hb_block, slot->ds_raw_block, reg->hr_block_bytes);
	cputime = le64_to_cpu(hb_block->hb_seq);
	if (slot->ds_last_time != cputime) {
		__be32 ipv4_address;
		__u8 ipv6_address[16];

		ipv4_address = cpu_to_be32(le32_to_cpu(hb_block->ip_addr.hb_ipv4_address));
		memcpy(ipv6_address, hb_block->ip_addr.hb_ipv6_address, sizeof(ipv6_address));

		my_node = o2nm_get_node_by_num(o2nm_this_node());
		if (!my_node) {
			mlog(ML_ERROR, "%s: Node not exist!\n", config_item_name(&reg->hr_item));
			goto out;
		}

		tmp_node = o2nm_get_node_by_num(slot->ds_node_num);
		if (!tmp_node) {
			if (my_node->nd_ipnet_type == 0)
				mlog(ML_ERROR, "%s: multi-cluster happened for node %d, "
						"ip %pI4 is not in my config.\n",
						config_item_name(&reg->hr_item), slot->ds_node_num,
						&ipv4_address);
			else
				mlog(ML_ERROR, "%s: multi-cluster happened for node %d, "
						"ip %pI6 is not in my config.\n",
						config_item_name(&reg->hr_item), slot->ds_node_num,
						ipv6_address);
			ret = 1;
			o2nm_node_put(my_node);
			goto out;
		}

		if (tmp_node->nd_ipnet_type == 0) {
			if (le32_to_cpu(hb_block->ip_addr.hb_ipv4_address) !=
					be32_to_cpu(tmp_node->nd_ipv4_address)) {
				mlog(ML_ERROR, "%s: multi-cluster happened for node %d, "
						"ip %pI4 does not match ip %pI4 in my config.\n",
						config_item_name(&reg->hr_item), slot->ds_node_num,
						&ipv4_address, &(tmp_node->nd_ipv4_address));
				ret = 1;
			}
		} else {
			if (memcmp(ipv6_address, tmp_node->nd_ipv6_address, sizeof(ipv6_address))) {
				mlog(ML_ERROR, "%s: multi-cluster happened for node %d, "
					"ip %pI6 does not match ip %pI6 in my config.\n",
					config_item_name(&reg->hr_item), slot->ds_node_num,
					ipv6_address, tmp_node->nd_ipv6_address);
				ret = 1;
			}
		}

		o2nm_node_put(tmp_node);
		o2nm_node_put(my_node);
	} else if (configure_map &&
			!test_bit(slot->ds_node_num, configure_map)) {
		if (le64_to_cpu(hb_block->hb_generation) != 0 &&
				le64_to_cpu(hb_block->hb_generation) != 1 &&
				le64_to_cpu(hb_block->hb_generation) != 2) {
			slot->ds_equal_samples++;
			slot->multicluster_checking = 1;
			mlog(ML_NOTICE,
				"%s: multicluster checking, slot %d, equal_samples %d.\n",
				config_item_name(&reg->hr_item),
				slot->ds_node_num, slot->ds_equal_samples);
		} else {
			slot->ds_equal_samples = 0;
			slot->multicluster_checking = 0;
		}

		if (slot->ds_equal_samples >= MOUNT_CHECK_COUNTS) {
			slot->multicluster_checking = 0;
			ret = o2hb_set_hb_generation(config_item_name(&reg->hr_item),
					slot->ds_node_num, 2);
			if (ret)
				mlog_errno(ret);
			ret = 0;
		}
	}

out:
	return ret;
}

static int o2hb_lock_type_inconsistency(struct o2hb_region *reg,
		struct o2hb_disk_slot *slot)
{
	struct o2hb_disk_heartbeat_block *hb_block = reg->hr_tmp_block;

	memcpy(hb_block, slot->ds_raw_block, reg->hr_block_bytes);
	if ((slot->ds_last_time != le64_to_cpu(hb_block->hb_seq)) &&
			reg->lock_type_adl != hb_block->hb_lock_type) {
		mlog(ML_ERROR,
			"%s: node %d lock type '%s' is inconsistent with my node %d lock type '%s'\n",
			config_item_name(&reg->hr_item),
			slot->ds_node_num, hb_block->hb_lock_type ? "ADL" : "DLM",
			o2nm_this_node(), reg->lock_type_adl ? "ADL" : "DLM");
		return 1;
	}

	return 0;
}

static int o2hb_highest_node(unsigned long *nodes, int numbits)
{
	return find_last_bit(nodes, numbits);
}

static int o2hb_lowest_node(unsigned long *nodes, int numbits)
{
	return find_first_bit(nodes, numbits);
}

static void o2hb_check_inconsist_slot(struct o2hb_region *reg, int slot_num)
{
	__be32 ipv4_address = 0;
	__u8 ipv6_address[16] = {0};
	struct o2hb_disk_slot *slot = reg->hr_slots[slot_num];

	if (slot->ds_raw_block->hb_ipnet_type == IPV4_TYPE) {
		ipv4_address = cpu_to_be32(le32_to_cpu(
				slot->ds_raw_block->ip_addr.hb_ipv4_address));
		mlog(ML_NOTICE, "node %d (ip: %pI4) is inconsistent with this node\n", slot_num,
				&ipv4_address);
	} else {
		memcpy(ipv6_address, slot->ds_raw_block->ip_addr.hb_ipv6_address,
				sizeof(ipv6_address));
		mlog(ML_NOTICE, "node %d (ip: %pI6) is inconsistent with this node\n", slot_num,
				ipv6_address);
	}

	/* If the node has been fenced, we don't need to check it again */
	if (fence_hb_fault && reg->mount_with_fence && !slot->has_been_fenced) {
		slot->ds_inconsist_samples++;
		if (slot->ds_inconsist_samples >= MOUNT_CHECK_COUNTS) {
			if (slot->ds_raw_block->hb_ipnet_type == IPV4_TYPE) {
				mlog(ML_ERROR,
					"node %d (ip: %pI4) is inconsistent with this node for %d times, fence it.\n",
					slot_num, &ipv4_address, MOUNT_CHECK_COUNTS);
			} else {
				mlog(ML_ERROR,
					"node %d (ip: %pI6) is inconsistent with this node for %d times, fence it.\n",
					slot_num, ipv6_address, MOUNT_CHECK_COUNTS);
			}
			reg->inconsistency_times = 0;
			slot->has_been_fenced = 1;
			mlog(ML_NOTICE, "%s: fence node %d\n", config_item_name(&reg->hr_item),
					slot->ds_node_num);
			o2hb_fence_scsi(reg->hr_bdev, slot->ds_node_num);
		}
	} else {
		reg->inconsistency_times++;
		if (reg->inconsistency_times >= MOUNT_CHECK_COUNTS) {
			if (slot->ds_raw_block->hb_ipnet_type == IPV4_TYPE) {
				mlog(ML_ERROR,
					"node %d (ip: %pI4) is inconsistent with this node for %d times, abort\n",
					slot_num, &ipv4_address, MOUNT_CHECK_COUNTS);
			} else {
				mlog(ML_ERROR,
					"node %d (ip: %pI6) is inconsistent with this node for %d times, abort\n",
					slot_num, ipv6_address, MOUNT_CHECK_COUNTS);
			}
			spin_lock(&o2hb_live_lock);
			reg->hr_inconsistency = 1;
			spin_unlock(&o2hb_live_lock);
			wake_up(&o2hb_steady_queue);
		}
	}
}

int o2hb_inconsistent_nodes_info_output(char *buf, int buf_len)
{
	size_t len = 0;

	if (g_inconsistent_nodes_info == NULL ||
		strlen(g_inconsistent_nodes_info) == 0)
		return 0;

	len = strlen(g_inconsistent_nodes_info);
	if (len >= buf_len) {
		mlog(ML_ERROR,
			"o2hb: inconsistent nodes info len %zu is not less than buf len %d\n",
			len, buf_len);
		return -EINVAL;
	}

	spin_lock(&o2hb_live_lock);
	memcpy(buf, g_inconsistent_nodes_info, len);
	spin_unlock(&o2hb_live_lock);
	buf[len] = '\0';

	return len;
}
EXPORT_SYMBOL_GPL(o2hb_inconsistent_nodes_info_output);

static int o2hb_save_inconsistent_nodes_info(struct o2hb_region *reg,
		unsigned long *configured_nodes)
{
	int i;
	int ret;
	int len = 0;
	struct o2hb_disk_heartbeat_block *hb_block;
	__be32 ipv4_address;

	i = -1;
	spin_lock(&o2hb_live_lock);
	while ((i = find_next_bit(configured_nodes,
				 O2NM_MAX_NODES, i + 1)) < O2NM_MAX_NODES) {
		if (!test_bit(i, reg->hr_inconsist_map))
			continue;

		hb_block = reg->hr_slots[i]->ds_raw_block;
		if (hb_block->hb_ipnet_type == IPV4_TYPE) {
			ipv4_address = cpu_to_be32(le32_to_cpu(hb_block->ip_addr.hb_ipv4_address));
			ret = snprintf(g_inconsistent_nodes_info + len, PAGE_SIZE - len, "%pI4,",
					&ipv4_address);
		} else {
			ret = snprintf(g_inconsistent_nodes_info + len, PAGE_SIZE - len, "%pI6,",
					hb_block->ip_addr.hb_ipv6_address);
		}

		if (ret >= PAGE_SIZE - len) {
			mlog(ML_ERROR, "%s: inconsistent nodes info exceed the maximum len %lu\n",
					config_item_name(&reg->hr_item), PAGE_SIZE);
			g_inconsistent_nodes_info[0] = '\0';
			ret = -EINVAL;
			goto out;
		}
		len += ret;
	}

	if (len > 0)
		g_inconsistent_nodes_info[len - 1] = '\0';
	ret = len;

out:
	spin_unlock(&o2hb_live_lock);
	return ret;
}

static int o2hb_do_disk_heartbeat(struct o2hb_region *reg)
{
	int i, ret, highest_node, lowest_node;
	int change = 0, heartbeat_checking = 0;
	unsigned long configured_nodes[BITS_TO_LONGS(O2NM_MAX_NODES)];
	unsigned long live_node_bitmap[BITS_TO_LONGS(O2NM_MAX_NODES)];

	ret = o2nm_configured_node_map(configured_nodes,
				       sizeof(configured_nodes));
	if (ret) {
		mlog_errno(ret);
		return ret;
	}

	/*
	 * If a node is not configured but is in the livemap, we still need
	 * to read the slot so as to be able to remove it from the livemap.
	 */
	memset(live_node_bitmap, 0, sizeof(live_node_bitmap));
	o2hb_fill_node_map(live_node_bitmap, sizeof(live_node_bitmap), NULL);
	i = -1;
	while ((i = find_next_bit(live_node_bitmap,
				  O2NM_MAX_NODES, i + 1)) < O2NM_MAX_NODES) {
		set_bit(i, configured_nodes);
	}

	highest_node = o2hb_highest_node(configured_nodes, O2NM_MAX_NODES);
	lowest_node = o2hb_lowest_node(configured_nodes, O2NM_MAX_NODES);
	if (highest_node >= O2NM_MAX_NODES || lowest_node >= O2NM_MAX_NODES) {
		mlog(ML_NOTICE, "o2hb: ocfs2_heartbeat: no configured nodes found!\n");
		return -EINVAL;
	}

	/* No sense in reading the slots of nodes that don't exist
	 * yet. Of course, if the node definitions have holes in them
	 * then we're reading an empty slot anyway... Consider this
	 * best-effort. */
	if (atomic_read(&reg->hr_steady_iterations))
		ret = o2hb_read_slots(reg, 0, reg->hr_blocks);
	else
		ret = o2hb_read_slots(reg, lowest_node, highest_node + 1);
	if (ret < 0) {
		mlog_errno(ret);
		return ret;
	}

	if (atomic_read(&reg->hr_steady_iterations)) {
		for (i = 0; i < reg->hr_blocks; ++i) {
			if (o2hb_check_slot_multicluster(reg, reg->hr_slots[i],
					configured_nodes)) {
				spin_lock(&o2hb_live_lock);
				reg->hr_multi_cluster = 1;
				spin_unlock(&o2hb_live_lock);
				wake_up(&o2hb_steady_queue);
				return 0;
			}

			if (o2hb_lock_type_inconsistency(reg, reg->hr_slots[i])) {
				spin_lock(&o2hb_live_lock);
				reg->hr_ltype_inconsistency = 1;
				spin_unlock(&o2hb_live_lock);
				wake_up(&o2hb_steady_queue);
				return 0;
			}
		}
	}

	if (reg->hr_inconsistency || reg->hr_ltype_inconsistency)
		return 0;

	/* With an up to date view of the slots, we can check that no
	  * other node has been improperly configured to heartbeat in
	  * our slot. */
	o2hb_check_own_slot(reg);

	i = -1;
	while ((i = find_next_bit(configured_nodes,
				 O2NM_MAX_NODES, i + 1)) < O2NM_MAX_NODES) {
		change |= o2hb_check_slot(reg, reg->hr_slots[i]);
	}

	/*
	 * In the follow cases, it needs to delay mount check heartbeat
	 * 1. check multicluster
	 * 2. pre check
	 * 3. check inconsistent map in adl lock
	 */
	if (atomic_read(&reg->hr_steady_iterations) != 0) {
		for (i = 0; i < reg->hr_blocks; ++i) {
			if (!test_bit(i, configured_nodes)) {
				if (reg->hr_slots[i]->multicluster_checking) {
					heartbeat_checking = 1;
					break;
				}
				continue;
			}

			/* pre check */
			if (test_bit(i, reg->hr_timeout_map)) {
				heartbeat_checking = 1;
				break;
			}

			/* check inconsistent map */
			if (!test_bit(i, reg->hr_inconsist_map))
				continue;

			heartbeat_checking = 1;
			o2hb_check_inconsist_slot(reg, i);
			break;
		}
		if (reg->hr_inconsistency) {
			if (o2hb_save_inconsistent_nodes_info(reg, configured_nodes) < 0)
				mlog(ML_ERROR, "%s: save mount fail info failed for device %s\n",
						config_item_name(&reg->hr_item), reg->hr_dev_name);
			return 0;
		}
	}

	/* let the person who launched us know when things are steady */
	if (!change && (atomic_read(&reg->hr_steady_iterations) != 0) &&
			!heartbeat_checking) {
		if (atomic_dec_and_test(&reg->hr_steady_iterations))
			wake_up(&o2hb_steady_queue);
	}

	return 0;
}

static int o2hb_write_disk_heartbeat_async(struct o2hb_region *reg)
{
	int ret = 0;
	struct page *page = NULL;
	struct o2hb_async_write_io_list *w_io = NULL;
	struct o2hb_disk_heartbeat_block *hb_block;

	page = alloc_page(GFP_NOFS);
	if (!page) {
		ret = -ENOMEM;
		mlog_errno(ret);
		goto bail;
	}

	w_io = kmem_cache_alloc(o2hb_inflight_io_cache, GFP_NOFS);
	if (!w_io) {
		ret = -ENOMEM;
		mlog_errno(ret);
		goto bail;
	}

	/* fill in the proper info for our next heartbeat. */
	hb_block = page_address(page);
	o2hb_fill_block(reg, reg->hr_generation, hb_block);

	w_io->page = page;

	/* fire off the write and we don't wait on this I/O. */
	ret = o2hb_issue_node_write_async(reg, w_io, o2nm_this_node());
	if (ret) {
		mlog_errno(ret);
		goto bail;
	}

	return 0;

bail:
	if (page)
		__free_page(page);
	if (w_io)
		kmem_cache_free(o2hb_inflight_io_cache, w_io);

	return ret;
}

static void o2hb_complete_lockres(struct o2hb_disk_slot *slot)
{
	struct o2hb_node_event event = {
		.hn_item = LIST_HEAD_INIT(event.hn_item),
	};
	struct o2nm_node *node = 0;

	node = o2nm_get_node_by_num(slot->ds_node_num);
	if (!node)
		return;
	spin_lock(&o2hb_live_lock);
	o2hb_queue_node_event(&event, O2HB_COMPLETE_LOCKRES_CB,
			node, slot->ds_node_num, config_item_name(&slot->reg->hr_item),
			slot->reg->hr_generation);
	spin_unlock(&o2hb_live_lock);

	o2hb_run_event_list(&event);

	o2nm_node_put(node);
}

/*
 * write heartbeat in a loop
 */
static int o2hb_write_thread(void *data)
{
	int i, ret;
	struct o2hb_region *reg = data;
	ktime_t before_hb, after_hb;
	struct task_struct *timeout_task;
	unsigned int elapsed_msec;
	unsigned long flags;

	mlog(ML_HEARTBEAT|ML_KTHREAD, "o2hb write thread running\n");

	set_user_nice(current, MIN_NICE);

	/* Pin node */
	ret = o2nm_depend_this_node();
	if (ret) {
		mlog(ML_ERROR, "Node has been deleted, ret = %d\n", ret);
		reg->hr_node_deleted = 1;
		goto bail;
	}

	/* If disk timeout when mynode is not in live_node_map */
	spin_lock_irqsave(&reg->hr_lock, flags);
	reg->hr_last_real_timeout_start = jiffies;
	queue_delayed_work(reg->o2hb_worker, &reg->hr_write_real_timeout_work,
			msecs_to_jiffies(O2HB_WRITE_REAL_TIMEOUT_MS));
	spin_unlock_irqrestore(&reg->hr_lock, flags);

	while (!kthread_should_stop() && !reg->hr_unclean_stop) {
		/* We track the time spent inside
		 * o2hb_write_disk_heartbeat_async so that we avoid more than
		 * hr_timeout_ms between disk writes. On busy systems
		 * this should result in a heartbeat which is less
		 * likely to time itself out. */
		before_hb = ktime_get_real();

		i = 0;
		do {
			ret = o2hb_write_disk_heartbeat_async(reg);
		} while (ret && ++i < 2);

		after_hb = ktime_get_real();

		elapsed_msec = (unsigned int)
				ktime_ms_delta(after_hb, before_hb);

		mlog(ML_HB_BIO,
		     "start = %lld, end = %lld, msec = %u, ret = %d\n",
		     before_hb, after_hb, elapsed_msec, ret);

		if (elapsed_msec < reg->hr_timeout_ms) {
			/* the kthread api has blocked signals for us so no
			 * need to record the return value. */
			msleep_interruptible(reg->hr_timeout_ms - elapsed_msec);
		}
	}

	wait_event(o2hb_inflight_io_queue, o2hb_inflight_io_empty(reg));
	o2hb_disarm_write_timeout(reg);

	spin_lock_irqsave(&reg->hr_lock, flags);
	timeout_task = reg->hb_timeout_task;
	reg->hb_timeout_task = NULL;
	spin_unlock_irqrestore(&reg->hr_lock, flags);
	if (timeout_task) {
		kthread_stop(timeout_task);
		/* put the last reference and release timeout_task */
		put_task_struct(timeout_task);
	}

bail:
	atomic_set(&reg->hr_write_hb_task_exited,  1);
	if (!atomic_read(&reg->hr_own_slot_populated))
		wake_up(&o2hb_write_thread_queue);
	mlog(ML_HEARTBEAT|ML_KTHREAD, "hb write thread exiting\n");

	return 0;
}

static int o2hb_inflight_io_empty(struct o2hb_region *reg)
{
	int ret;
	unsigned long flags;

	spin_lock_irqsave(&reg->hr_lock, flags);
	ret = list_empty(&reg->hr_inflight_io);
	spin_unlock_irqrestore(&reg->hr_lock, flags);

	return ret;
}

/*
 * we ride the region ref that the region dir holds.  before the region
 * dir is removed and drops it ref it will wait to tear down this
 * thread.
 */
static int o2hb_thread(void *data)
{
	int i, ret;
	struct o2hb_region *reg = data;
	struct o2hb_io_wait_ctxt write_wc;
	ktime_t before_hb, after_hb;
	unsigned int elapsed_msec;
	struct workqueue_struct *tmp_o2hb_worker;
	int fence_is_invalid;
	unsigned long flags;

	mlog(ML_HEARTBEAT|ML_KTHREAD, "o2hb thread running\n");

	set_user_nice(current, MIN_NICE);

	while (!atomic_read(&reg->hr_write_hb_task_exited) ||
			(!kthread_should_stop() && !reg->hr_unclean_stop &&
				!reg->hr_multi_cluster)) {
		/* We track the time spent inside
		 * o2hb_do_disk_heartbeat so that we avoid more than
		 * hr_timeout_ms between disk writes. On busy systems
		 * this should result in a heartbeat which is less
		 * likely to time itself out. */
		before_hb = ktime_get_real();

		i = 0;
		do {
			ret = o2hb_do_disk_heartbeat(reg);
		} while (ret && ++i < 2);

		after_hb = ktime_get_real();

		elapsed_msec = (unsigned int)
				ktime_ms_delta(after_hb, before_hb);

		mlog(ML_HB_BIO,
		     "start = %lld, end = %lld, msec = %u, ret = %d\n",
		     before_hb, after_hb, elapsed_msec, ret);

		if (elapsed_msec < reg->hr_timeout_ms) {
			/* the kthread api has blocked signals for us so no
			 * need to record the return value. */
			msleep_interruptible(reg->hr_timeout_ms - elapsed_msec);
		}
	}

	/* flush o2hb_worker before o2hb_thread exit */
	spin_lock_irqsave(&reg->hr_lock, flags);
	tmp_o2hb_worker = reg->o2hb_worker;
	reg->o2hb_worker = NULL;
	spin_unlock_irqrestore(&reg->hr_lock, flags);
	if (tmp_o2hb_worker)
		destroy_workqueue(tmp_o2hb_worker);

	fence_is_invalid = o2nm_fence_method_is_invalid();
	/* in case of fence, we set o2hb region to invalid.
	 * call o2hb_shutdown_slot to queue node event */
	for (i = 0; (fence_is_invalid || !reg->hr_fence)
			&& i < reg->hr_blocks; i++) {
		if (reg->hr_unclean_stop)
			if (i == o2nm_this_node())
				continue;
		o2hb_shutdown_slot(reg->hr_slots[i]);
	}

	if (reg->hr_inconsistency || reg->hr_ltype_inconsistency ||
			reg->hr_multi_cluster)
		goto out;

	if (atomic_read(&reg->hr_steady_iterations) != 0) {
		mlog(ML_ERROR, "%s: mount.ocfs2 exit when disk timeout\n",
				config_item_name(&reg->hr_item));
		reg->hr_disk_timeout = 1;
		wake_up(&o2hb_steady_queue);
		goto out;
	}

	/* Explicit down notification - avoid forcing the other nodes
	 * to timeout on this region when we could just as easily
	 * write a clear generation - thus indicating to them that
	 * this node has left this region.
	 *
	 * XXX: Should we skip this on unclean_stop? */
	if (reg->hr_unclean_stop) {
		/* o2hb_thread exit because of ocfs2 invalid, lockres need not wait */
		/* in case of fence, we want to write generation 0x1 */
		if (!reg->hr_fence) {
			o2hb_complete_lockres(reg->hr_slots[o2nm_this_node()]);
			goto out;
		} else if (fence_is_invalid) {
			o2hb_complete_lockres(reg->hr_slots[o2nm_this_node()]);
		}
	}

	if (reg->hr_fence)
		o2hb_prepare_block(reg, 1);
	else
		o2hb_prepare_block(reg, 0);
	ret = o2hb_issue_node_write(reg, &write_wc, o2nm_this_node());
	if (ret == 0)
		o2hb_wait_on_io(&write_wc);
	else
		mlog_errno(ret);

out:
	/* Unpin node */
	/* if disk timeout, o2hb_heartbeat_group_drop_item will Unpin node */
	if (!reg->hr_unclean_stop)
		o2nm_undepend_this_node();

	mlog(ML_HEARTBEAT|ML_KTHREAD, "hb thread exiting\n");

	return 0;
}

#ifdef CONFIG_DEBUG_FS
static int o2hb_debug_open(struct inode *inode, struct file *file)
{
	struct o2hb_debug_buf *db = inode->i_private;
	struct o2hb_region *reg;
	unsigned long map[BITS_TO_LONGS(O2NM_MAX_NODES)];
	unsigned long lts;
	char *buf = NULL;
	int i = -1;
	int out = 0;
	unsigned long flags;

	/* max_nodes should be the largest bitmap we pass here */
	BUG_ON(sizeof(map) < db->db_size);

	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
	if (!buf)
		goto bail;

	switch (db->db_type) {
	case O2HB_DB_TYPE_LIVENODES:
	case O2HB_DB_TYPE_LIVEREGIONS:
	case O2HB_DB_TYPE_QUORUMREGIONS:
	case O2HB_DB_TYPE_FAILEDREGIONS:
		spin_lock(&o2hb_live_lock);
		memcpy(map, db->db_data, db->db_size);
		spin_unlock(&o2hb_live_lock);
		break;

	case O2HB_DB_TYPE_REGION_LIVENODES:
		spin_lock(&o2hb_live_lock);
		reg = (struct o2hb_region *)db->db_data;
		spin_lock_irqsave(&reg->hr_lock, flags);
		memcpy(map, reg->hr_live_node_bitmap, db->db_size);
		spin_unlock_irqrestore(&reg->hr_lock, flags);
		spin_unlock(&o2hb_live_lock);
		break;

	case O2HB_DB_TYPE_REGION_NUMBER:
		reg = (struct o2hb_region *)db->db_data;
		out += scnprintf(buf + out, PAGE_SIZE - out, "%d\n",
				reg->hr_region_num);
		goto done;

	case O2HB_DB_TYPE_REGION_ELAPSED_TIME:
		reg = (struct o2hb_region *)db->db_data;
		lts = reg->hr_last_real_timeout_start;
		/* If 0, it has never been set before */
		if (lts)
			lts = jiffies_to_msecs(jiffies - lts);
		out += scnprintf(buf + out, PAGE_SIZE - out, "%lu\n", lts);
		goto done;

	case O2HB_DB_TYPE_REGION_PINNED:
		reg = (struct o2hb_region *)db->db_data;
		out += scnprintf(buf + out, PAGE_SIZE - out, "%u\n",
				!!reg->hr_item_pinned);
		goto done;

	case O2HB_DB_TYPE_REGION_INFLIGHT_HB_IO:
		reg = (struct o2hb_region *)db->db_data;
		/* traverse the hb io list */
		out += o2hb_traverse_hb_io(reg, buf);
		goto done;

	default:
		goto done;
	}

	while ((i = find_next_bit(map, db->db_len, i + 1)) < db->db_len)
		out += scnprintf(buf + out, PAGE_SIZE - out, "%d ", i);
	out += scnprintf(buf + out, PAGE_SIZE - out, "\n");

done:
	i_size_write(inode, out);

	file->private_data = buf;

	return 0;
bail:
	return -ENOMEM;
}

static int o2hb_debug_release(struct inode *inode, struct file *file)
{
	kfree(file->private_data);
	return 0;
}

static ssize_t o2hb_debug_read(struct file *file, char __user *buf,
				 size_t nbytes, loff_t *ppos)
{
	return simple_read_from_buffer(buf, nbytes, ppos, file->private_data,
				       i_size_read(file->f_mapping->host));
}

static int o2hb_state_seq_show(struct seq_file *seq, void *data)
{
	struct o2hb_region *reg;
	struct o2nm_node *node;
	struct o2hb_disk_slot *slot;
	u8 nd_state;
	u16 nd_num;
	char nd_ipv4[INET_ADDRSTRLEN] = {0};
	char nd_ipv6[INET6_ADDRSTRLEN] = {0};

	reg = (struct o2hb_region *)seq->private;
	if (reg == NULL)
		return 0;

	seq_printf(seq, "#num IP              ds_equal_samples state\n");
	for (nd_num = 0; nd_num < O2NM_MAX_NODES; nd_num++) {
		node = o2nm_get_node_by_num(nd_num);
		if (node) {
			nd_state = reg->o2nodes_state[nd_num];

			if (nd_state == NODE_STATE_QUORUM_DELAY_2S)
				nd_state = NODE_STATE_QUORUM;

			slot = reg->hr_slots[nd_num];
			if (node->nd_ipnet_type == IPV4_TYPE) {
				sprintf(nd_ipv4, "%pI4", &node->nd_ipv4_address);
				seq_printf(seq, "%-4u %-15s %-16u %u\n", nd_num, nd_ipv4,
						slot->ds_equal_samples, nd_state);
			} else {
				sprintf(nd_ipv6, "%pI6", node->nd_ipv6_address);
				seq_printf(seq, "%-4u %-15s %-16u %u\n", nd_num, nd_ipv6,
						slot->ds_equal_samples, nd_state);
			}
			o2nm_node_put(node);
		}
	}

	return 0;
}

static int o2hb_state_debug_open(struct inode *inode, struct file *file)
{
	struct o2hb_region *reg;

	reg = (struct o2hb_region *)inode->i_private;

	return single_open(file, o2hb_state_seq_show, reg);
}
#else
static int o2hb_debug_open(struct inode *inode, struct file *file)
{
	return 0;
}
static int o2hb_debug_release(struct inode *inode, struct file *file)
{
	return 0;
}
static ssize_t o2hb_debug_read(struct file *file, char __user *buf,
			       size_t nbytes, loff_t *ppos)
{
	return 0;
}

static int o2hb_state_debug_open(struct inode *inode, struct file *file)
{
	return 0;
}

#endif  /* CONFIG_DEBUG_FS */

static const struct file_operations o2hb_debug_fops = {
	.open =		o2hb_debug_open,
	.release =	o2hb_debug_release,
	.read =		o2hb_debug_read,
	.llseek =	generic_file_llseek,
};

static const struct file_operations o2state_debug_fops = {
	.open = o2hb_state_debug_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
};

static int o2hb_send_one_query(u16 err_node, u16 target_node, int *response)
{
	struct o2hb_conn_query query_msg;

	mlog(ML_HEARTBEAT, "Sending query message to node %u for err node %u\n",
			target_node, err_node);

	memset(&query_msg, 0, sizeof(query_msg));
	query_msg.node_idx = cpu_to_be16(o2nm_this_node());
	query_msg.err_node = cpu_to_be16(err_node);

	return o2net_send_message(O2HB_CONN_FAILURE_QUERY,
			O2HB_MOD_KEY, &query_msg, sizeof(query_msg),
			target_node, 0, response);
}

int o2hb_query_handler(struct o2net_msg *msg)
{
	struct o2hb_region *reg;
	struct o2hb_disk_slot *slot;
	struct o2nm_node *node;
	struct o2hb_node_event event_up = {
		.hn_item = LIST_HEAD_INIT(event_up.hn_item),
	};
	struct o2hb_node_event event_down = {
		.hn_item = LIST_HEAD_INIT(event_down.hn_item),
	};
	int response = 0, queued = 0;
	int generation_changed = 0;
	u16 node_num;
	unsigned long flags;
	struct o2hb_query_join *request_msg = NULL;

	if (be16_to_cpu(msg->data_len) != sizeof(struct o2hb_query_join))
		return response;
	request_msg = (struct o2hb_query_join *)msg->buf;

	node_num = be16_to_cpu(request_msg->node_num);
	node = o2nm_get_node_by_num(node_num);
	if (!node)
		return response;

	spin_lock(&o2hb_live_lock);
	list_for_each_entry(reg, &o2hb_all_regions, hr_all_item) {
		if (reg->hr_item_dropped || !config_item_name(&reg->hr_item))
			continue;
		if (!memcmp(request_msg->uuid_name, config_item_name(&reg->hr_item),
				strlen(config_item_name(&reg->hr_item))) &&
				!reg->hr_unclean_stop) {
			if (test_bit(o2nm_this_node(), reg->hr_live_node_bitmap)) {
				response = 1;
				/* move it here to avoid NULL pointer dereference
				 * when mount fails */
				slot = reg->hr_slots[node_num];
				mlog(ML_NOTICE,
						"%s: Node %d changed generation: 0x%llx => 0x%llx\n",
						config_item_name(&reg->hr_item),
						slot->ds_node_num,
						(long long)slot->ds_last_generation,
						(long long)be64_to_cpu(request_msg->genaration));
				spin_lock(&slot->ds_lock);
				/* update slot last generation */
				if (slot->ds_last_generation !=
						be64_to_cpu(request_msg->genaration)) {
					/* This node send generation to me when he mounting,
					 * we record it, in case of o2hb_thread and this thread
					 * conflict */
					slot->ds_generation_changed = 1;
					generation_changed = 1;
				}
				slot->ds_last_generation = be64_to_cpu(request_msg->genaration);
				spin_unlock(&slot->ds_lock);
				if (list_empty(&slot->ds_live_item)) {
					mlog(ML_NOTICE, "Node %d (id 0x%llx) joined my region %s\n",
							slot->ds_node_num,
							(long long)slot->ds_last_generation,
							config_item_name(&reg->hr_item));
					/* After add other node in live node map, i should clear
					 * its bit in hr_timeout_map so that mount successfully. */
					clear_bit(slot->ds_node_num, reg->hr_timeout_map);
					spin_lock_irqsave(&reg->hr_lock, flags);
					set_bit(slot->ds_node_num, reg->hr_live_node_bitmap);
					reg->hr_heartbeating++;
					spin_unlock_irqrestore(&reg->hr_lock, flags);

					if (!list_empty(&o2hb_live_slots[slot->ds_node_num])) {
						o2hb_queue_node_event(&event_up, O2HB_REGION_UP_CB,
								node, slot->ds_node_num,
								config_item_name(&reg->hr_item),
								slot->ds_last_generation);
					} else {
						/* first on the list generates a callback */
						mlog(ML_NOTICE,
							"Add node %d to live nodes bitmap, uuid %s\n",
							slot->ds_node_num,
							config_item_name(&reg->hr_item));

						set_bit(slot->ds_node_num, o2hb_live_node_bitmap);
						o2hb_queue_node_event(&event_up, O2HB_NODE_UP_CB,
								node,
								slot->ds_node_num,
								config_item_name(&reg->hr_item),
								slot->ds_last_generation);
					}

					o2quo_hb_up(slot->ds_node_num, reg->lock_type_adl);

					list_add_tail(&slot->ds_live_item,
							&o2hb_live_slots[slot->ds_node_num]);

					slot->ds_equal_samples = 0;
					slot->ds_fake_timeout  = 0;
					slot->ds_quorum_fence_samples = 0;

					o2hb_node_state_change(reg, slot->ds_node_num,
						NODE_STATE_MOUNTED);
				} else {
					if (generation_changed) {
						mlog(ML_NOTICE, "Node %d may be in domain %s,"
								"we let it leave domain first,"
								"and then add it again by dlm join request\n",
								slot->ds_node_num,
								config_item_name(&reg->hr_item));
						/* If query node is already in my region,
						 * firstly we remove it from domain map,
						 * and then add it again */
						o2hb_queue_node_event(&event_down,
								O2HB_REGION_DOWN_CB,
								node, slot->ds_node_num,
								config_item_name(&reg->hr_item),
								slot->ds_last_generation);
					}
					/* If query node in my region,
					 * but not in dlm->live_nodes_map,
					 * force adding query node in it */
					o2hb_queue_node_event(&event_up, O2HB_REGION_UP_CB,
							node, slot->ds_node_num,
							config_item_name(&reg->hr_item),
							slot->ds_last_generation);
				}

				queued = 1;
			}
			goto out;
		}
	}

out:
	spin_unlock(&o2hb_live_lock);

	/* run event list will perform all o2hb_node_events, include event_down */
	if (queued)
		o2hb_run_event_list(&event_up);

	if (node)
		o2nm_node_put(node);

	return response;
}

int o2hb_conn_failure_query_handler(struct o2net_msg *msg, u32 len,
		void *data, void **ret_data)
{
	unsigned int err_node, node_idx;
	int response;
	struct o2hb_conn_query *err_msg =
			(struct o2hb_conn_query *)msg->buf;

	node_idx = be16_to_cpu(err_msg->node_idx);
	err_node = be16_to_cpu(err_msg->err_node);

	response = o2quo_conn_check_err(err_node);

	mlog(0,
		"Node %u sent a query message for err node: %u,and we response %d\n",
		node_idx, err_node, response);

	return response;
}

static void o2hb_unregister_net_handlers(void)
{
	o2net_unregister_handler_list(&o2hb_handlers);
}

static int o2hb_register_net_handlers(void)
{
	return o2net_register_handler(O2HB_CONN_FAILURE_QUERY, O2HB_MOD_KEY,
					sizeof(struct o2hb_conn_query),
					sizeof(struct o2hb_conn_query),
					o2hb_conn_failure_query_handler,
					NULL, NULL, &o2hb_handlers);
}

static void o2hb_destroy_inflight_io_cache(void)
{
	kmem_cache_destroy(o2hb_inflight_io_cache);
}

static void o2hb_proc_exit(void);

void o2hb_exit(void)
{
	o2hb_unregister_net_handlers();
	debugfs_remove(o2hb_debug_failedregions);
	debugfs_remove(o2hb_debug_quorumregions);
	debugfs_remove(o2hb_debug_liveregions);
	debugfs_remove(o2hb_debug_livenodes);
	debugfs_remove(o2hb_debug_nodedown);
	debugfs_remove(o2hb_debug_dir);
	debugfs_remove(o2state_debug_dir);
	kfree(o2hb_db_livenodes);
	kfree(o2hb_db_liveregions);
	kfree(o2hb_db_quorumregions);
	kfree(o2hb_db_failedregions);
	kfree(g_inconsistent_nodes_info);

	o2hb_destroy_inflight_io_cache();
	o2hb_proc_exit();
}

static struct dentry *o2hb_debug_create(const char *name, struct dentry *dir,
					struct o2hb_debug_buf **db, int db_len,
					int type, int size, int len, void *data)
{
	*db = kmalloc(db_len, GFP_KERNEL);
	if (!*db)
		return NULL;

	(*db)->db_type = type;
	(*db)->db_size = size;
	(*db)->db_len = len;
	(*db)->db_data = data;

	return debugfs_create_file(name, S_IFREG|S_IRUSR, dir, *db,
				   &o2hb_debug_fops);
}

void o2hb_clear_fence_state(const char *region_uuid)
{
	if (!o2hb_check_heartbeating_type(region_uuid))
		fence_invalid = 0;
}
EXPORT_SYMBOL_GPL(o2hb_clear_fence_state);

static int min_state;
static int max_state = 1;

static unsigned int min_invalid_hung_timeout;
static unsigned int max_invalid_hung_timeout = INT_MAX;

static struct ctl_table ocfs2_nm_table[] = {
	{
		.procname	= "hb_ctl_path",
		.data		= ocfs2_hb_ctl_path,
		.maxlen		= OCFS2_MAX_HB_CTL_PATH,
		.mode		= 0644,
		.proc_handler	= proc_dostring,
	},
	{ }
};

static struct ctl_table ocfs2_mod_table[] = {
	{
		.procname	= "nm",
		.data		= NULL,
		.maxlen		= 0,
		.mode		= 0555,
		.child		= ocfs2_nm_table
	},
	{
		.procname = "disable_fence",
		.maxlen = sizeof(int),
		.mode = 0644,
		.proc_handler = proc_dointvec_minmax,
		.data = &disable_fence,
		.extra1 = &min_state,
		.extra2 = &max_state,
	},
	{
		.procname = "fence_invalid",
		.maxlen = sizeof(int),
		.mode = 0644,
		.proc_handler = proc_dointvec_minmax,
		.data = &fence_invalid,
		.extra1 = &min_state,
		.extra2 = &max_state,
	},
	{
		.procname = "invalid_hung_timeout",
		.maxlen = sizeof(int),
		.mode = 0644,
		.proc_handler = proc_dointvec_minmax,
		.data = &invalid_hung_timeout,
		.extra1 = &min_invalid_hung_timeout,
		.extra2 = &max_invalid_hung_timeout,
	},
	{
		.procname = "fence_hb_fault",
		.maxlen = sizeof(int),
		.mode = 0644,
		.proc_handler = proc_dointvec_minmax,
		.data = &fence_hb_fault,
		.extra1 = &min_state,
		.extra2 = &max_state,
	},
	{ }
};

static struct ctl_table ocfs2_kern_table[] = {
	{
		.procname	= "ocfs2",
		.data		= NULL,
		.maxlen		= 0,
		.mode		= 0555,
		.child		= ocfs2_mod_table,
	},
	{ }
};

static struct ctl_table ocfs2_root_table[] = {
	{
		.procname	= "fs",
		.data		= NULL,
		.maxlen		= 0,
		.mode		= 0555,
		.child		= ocfs2_kern_table
	},
	{ }
};

static int o2hb_init_inflight_io_cache(void)
{
	o2hb_inflight_io_cache = kmem_cache_create("o2hb_inflight_io",
					  sizeof(struct o2hb_async_write_io_list),
					  0, SLAB_HWCACHE_ALIGN,
					  NULL);
	if (!o2hb_inflight_io_cache)
		return -ENOMEM;
	return 0;
}

static struct ctl_table_header *ocfs2_table_header;

static int o2hb_proc_init(void)
{
	ocfs2_table_header = register_sysctl_table(ocfs2_root_table);
	if (!ocfs2_table_header) {
		mlog(ML_ERROR,
			"ERROR: ocfs2 heartbeat: unable to register sysctl\n");
		return -ENOMEM; /* or something. */
	}
	return 0;
}

static void o2hb_proc_exit(void)
{
	if (ocfs2_table_header)
		unregister_sysctl_table(ocfs2_table_header);
}

static int o2hb_notify_node_down(struct o2nm_node *node)
{
	unsigned long flags;
	struct o2hb_node_event event = {
		.hn_item = LIST_HEAD_INIT(event.hn_item),
	};
	struct o2hb_region *reg;
	struct o2hb_disk_slot *slot;

	if (node->nd_num == o2nm_this_node())
		return -EINVAL;

	spin_lock(&o2hb_live_lock);
	list_for_each_entry(reg, &o2hb_all_regions, hr_all_item) {
		if (reg->hr_item_dropped || !config_item_name(&reg->hr_item))
			continue;

		slot = reg->hr_slots[node->nd_num];
		if (list_empty(&slot->ds_live_item))
			continue;

		spin_lock_irqsave(&reg->hr_lock, flags);
		reg->hr_qs_effect_nodes[slot->ds_node_num] = 0;
		reg->hr_qs_lowest_node[slot->ds_node_num] = O2NM_MAX_NODES;
		clear_bit(slot->ds_node_num, reg->hr_live_node_bitmap);
		reg->hr_heartbeating--;
		spin_unlock_irqrestore(&reg->hr_lock, flags);

		clear_bit(slot->ds_node_num, reg->hr_inconsist_map);

		spin_lock(&slot->ds_lock);
		slot->ds_fake_timeout = 0;
		slot->ds_pre_checked = 0;
		slot->ds_generation_changed = 0;
		spin_unlock(&slot->ds_lock);
		list_del_init(&slot->ds_live_item);

		if (!list_empty(&o2hb_live_slots[slot->ds_node_num])) {
			o2hb_queue_node_event(&event, O2HB_REGION_DOWN_CB,
					node, slot->ds_node_num,
					config_item_name(&reg->hr_item),
					slot->ds_last_generation);
		} else {
			mlog(ML_NOTICE,
				"Remove node %d from live nodes bitmap, uuid %s\n",
				slot->ds_node_num, config_item_name(&reg->hr_item));
			clear_bit(slot->ds_node_num, o2hb_live_node_bitmap);
			o2hb_queue_node_event(&event, O2HB_NODE_DOWN_CB,
					node, slot->ds_node_num,
					config_item_name(&reg->hr_item),
					slot->ds_last_generation);
		}

		o2quo_hb_down(slot->ds_node_num, reg->lock_type_adl);

		spin_unlock(&o2hb_live_lock);
		o2hb_run_event_list(&event);
		spin_lock(&o2hb_live_lock);
	}
	spin_unlock(&o2hb_live_lock);

	return 0;
}

static ssize_t debug_nodedown_write(struct file *filp,
		const char __user *ubuf, size_t cnt, loff_t *ppos)
{
	char node_ip_addr[INET6_ADDRSTRLEN] = {0};
	int ret, i;
	unsigned int octets[4];
	__be32 ipv4_addr = 0;
	u8 ipv6_addr[16] = {0};
	struct o2nm_node *node = NULL;

	if (cnt >= INET6_ADDRSTRLEN)
		return -EINVAL;

	if (copy_from_user(&node_ip_addr, ubuf, cnt))
		return -EFAULT;

	if (strstr(node_ip_addr, ".")) {
		ret = sscanf(node_ip_addr, "%3u.%3u.%3u.%3u", &octets[3], &octets[2],
				&octets[1], &octets[0]);
		if (ret != 4)
			return -EINVAL;

		for (i = 0; i < 4; i++) {
			if (octets[i] > 0xFF)
				return -ERANGE;
			be32_add_cpu(&ipv4_addr, octets[i] << (i * 8));
		}
		node = o2nm_get_node_by_ipv4(ipv4_addr);
	} else if (strstr(node_ip_addr, ":")) {
		ret = in6_pton(node_ip_addr, cnt, ipv6_addr, -1, NULL);
		if (ret != 1) {
			mlog(ML_ERROR,
				"in6_pton failed, ret %d, ipv6 %s, count %zu\n",
				ret, node_ip_addr, cnt);
			return -EINVAL;
		}
		node = o2nm_get_node_by_ipv6(ipv6_addr);
	}

	if (!node)
		return -EINVAL;

	ret = o2hb_notify_node_down(node);
	o2nm_node_put(node);
	if (ret)
		return ret;

	(*ppos)++;

	return cnt;
}

static ssize_t default_read_file(struct file *file, char __user *buf,
		size_t count, loff_t *ppos)
{
	return 0;
}

static int default_open(struct inode *inode, struct file *file)
{
	if (inode->i_private)
		file->private_data = inode->i_private;

	return 0;
}

static const struct file_operations debug_nodedown_fops = {
	.write	=	debug_nodedown_write,
	.open	=	default_open,
	.read	=	default_read_file,
	.llseek	=	noop_llseek,
};

static int o2hb_debug_init(void)
{
	int ret = -ENOMEM;

	o2hb_debug_dir = debugfs_create_dir(O2HB_DEBUG_DIR, NULL);
	if (!o2hb_debug_dir) {
		mlog_errno(ret);
		goto bail;
	}

	o2hb_debug_livenodes = o2hb_debug_create(O2HB_DEBUG_LIVENODES,
						 o2hb_debug_dir,
						 &o2hb_db_livenodes,
						 sizeof(*o2hb_db_livenodes),
						 O2HB_DB_TYPE_LIVENODES,
						 sizeof(o2hb_live_node_bitmap),
						 O2NM_MAX_NODES,
						 o2hb_live_node_bitmap);
	if (!o2hb_debug_livenodes) {
		mlog_errno(ret);
		goto bail;
	}

	o2hb_debug_liveregions = o2hb_debug_create(O2HB_DEBUG_LIVEREGIONS,
						   o2hb_debug_dir,
						   &o2hb_db_liveregions,
						   sizeof(*o2hb_db_liveregions),
						   O2HB_DB_TYPE_LIVEREGIONS,
						   sizeof(o2hb_live_region_bitmap),
						   O2NM_MAX_REGIONS,
						   o2hb_live_region_bitmap);
	if (!o2hb_debug_liveregions) {
		mlog_errno(ret);
		goto bail;
	}

	o2hb_debug_quorumregions =
			o2hb_debug_create(O2HB_DEBUG_QUORUMREGIONS,
					  o2hb_debug_dir,
					  &o2hb_db_quorumregions,
					  sizeof(*o2hb_db_quorumregions),
					  O2HB_DB_TYPE_QUORUMREGIONS,
					  sizeof(o2hb_quorum_region_bitmap),
					  O2NM_MAX_REGIONS,
					  o2hb_quorum_region_bitmap);
	if (!o2hb_debug_quorumregions) {
		mlog_errno(ret);
		goto bail;
	}

	o2hb_debug_failedregions =
			o2hb_debug_create(O2HB_DEBUG_FAILEDREGIONS,
					  o2hb_debug_dir,
					  &o2hb_db_failedregions,
					  sizeof(*o2hb_db_failedregions),
					  O2HB_DB_TYPE_FAILEDREGIONS,
					  sizeof(o2hb_failed_region_bitmap),
					  O2NM_MAX_REGIONS,
					  o2hb_failed_region_bitmap);
	if (!o2hb_debug_failedregions) {
		mlog_errno(ret);
		goto bail;
	}

	o2state_debug_dir = debugfs_create_dir(O2STATE_DEBUG_DIR, NULL);
	if (!o2state_debug_dir) {
		mlog_errno(ret);
		goto bail;
	}

	o2hb_debug_nodedown = debugfs_create_file(O2HB_DEBUG_NODEDOWN,
			S_IRUSR|S_IWUSR, o2hb_debug_dir, NULL, &debug_nodedown_fops);
	if (!o2hb_debug_nodedown) {
		mlog_errno(ret);
		goto bail;
	}

	ret = 0;
bail:
	if (ret)
		o2hb_exit();

	return ret;
}

int o2hb_init(void)
{
	int i;
	int status;

	for (i = 0; i < ARRAY_SIZE(o2hb_callbacks); i++)
		INIT_LIST_HEAD(&o2hb_callbacks[i].list);

	for (i = 0; i < ARRAY_SIZE(o2hb_live_slots); i++)
		INIT_LIST_HEAD(&o2hb_live_slots[i]);

	INIT_LIST_HEAD(&o2hb_node_events);

	memset(o2hb_live_node_bitmap, 0, sizeof(o2hb_live_node_bitmap));
	memset(o2hb_region_bitmap, 0, sizeof(o2hb_region_bitmap));
	memset(o2hb_live_region_bitmap, 0, sizeof(o2hb_live_region_bitmap));
	memset(o2hb_quorum_region_bitmap, 0, sizeof(o2hb_quorum_region_bitmap));
	memset(o2hb_failed_region_bitmap, 0, sizeof(o2hb_failed_region_bitmap));

	o2hb_dependent_users = 0;

	status = o2hb_init_inflight_io_cache();
	if (status)
		return status;

	status = o2hb_register_net_handlers();
	if (status) {
		o2hb_destroy_inflight_io_cache();
		return status;
	}

	/* Added by jiangyiwen */
	status = o2hb_proc_init();
	if (status) {
		o2hb_destroy_inflight_io_cache();
		o2hb_unregister_net_handlers();
		return status;
	}

	status = o2hb_debug_init();
	if (status)
		return status;

	g_inconsistent_nodes_info = kzalloc(PAGE_SIZE, GFP_KERNEL);
	if (g_inconsistent_nodes_info == NULL) {
		o2hb_exit();
		status = -ENOMEM;
		mlog_errno(status);
	}

	return status;
}

/* if we're already in a callback then we're already serialized by the sem */
static void o2hb_fill_node_map_from_callback(unsigned long *map,
					     unsigned int bytes, const char *uuid)
{
	struct o2hb_region *reg;

	BUG_ON(bytes < (BITS_TO_LONGS(O2NM_MAX_NODES) * sizeof(unsigned long)));

	spin_lock(&o2hb_live_lock);
	if (!uuid) {
		memcpy(map, &o2hb_live_node_bitmap, bytes);
		spin_unlock(&o2hb_live_lock);
		return;
	}

	list_for_each_entry(reg, &o2hb_all_regions, hr_all_item) {
		if (reg->hr_item_dropped || !config_item_name(&reg->hr_item))
			continue;
		if (!memcmp(uuid, config_item_name(&reg->hr_item),
				strlen(config_item_name(&reg->hr_item)))) {
			memcpy(map, reg->hr_live_node_bitmap, bytes);
			break;
		}
	}
	spin_unlock(&o2hb_live_lock);
}

void o2hb_fill_node_map_and_generation(unsigned long *map, unsigned int bytes,
		u32 *HBGen[], const char *uuid)
{
	struct o2hb_region *reg;
	struct o2hb_disk_slot *slot;
	int num = 0, found = 0;

	mlog(ML_NOTICE, "%s\n", uuid);
	/* callers want to serialize this map and callbacks so that they
	  * can trust that they don't miss nodes coming to the party */
	down_read(&o2hb_callback_sem);

	BUG_ON(bytes < (BITS_TO_LONGS(O2NM_MAX_NODES) * sizeof(unsigned long)));

	spin_lock(&o2hb_live_lock);
	list_for_each_entry(reg, &o2hb_all_regions, hr_all_item) {
		if (reg->hr_item_dropped || !config_item_name(&reg->hr_item))
			continue;
		if (!memcmp(uuid, config_item_name(&reg->hr_item),
				strlen(config_item_name(&reg->hr_item)))) {
			found = 1;
			break;
		}
	}

	if (found == 0) {
		mlog(ML_ERROR, "cannot find region %s\n", uuid);
		goto unlock;
	}

	memcpy(map, reg->hr_live_node_bitmap, bytes);
	num = 0;
	while (1) {
		num = find_next_bit(map, O2NM_MAX_NODES, num);
		if (num >= O2NM_MAX_NODES)
			break;
		slot = reg->hr_slots[num];
		spin_lock(&slot->ds_lock);
		*HBGen[num] = (u32)slot->ds_last_generation;
		spin_unlock(&slot->ds_lock);
		mlog(ML_NOTICE, "%s: num %d, generation 0x%x\n",
				uuid, num, *HBGen[num]);
		num++;
	}
unlock:
	spin_unlock(&o2hb_live_lock);
	up_read(&o2hb_callback_sem);
}
EXPORT_SYMBOL_GPL(o2hb_fill_node_map_and_generation);

int o2hb_get_livenode_nr(const char *uuid)
{
	struct o2hb_region *reg;
	unsigned long flags;
	int num = 0;

	spin_lock(&o2hb_live_lock);
	list_for_each_entry(reg, &o2hb_all_regions, hr_all_item) {
		if (reg->hr_item_dropped || !config_item_name(&reg->hr_item))
			continue;
		if (!memcmp(uuid, config_item_name(&reg->hr_item),
				strlen(config_item_name(&reg->hr_item)))) {
			spin_lock_irqsave(&reg->hr_lock, flags);
			num = reg->hr_heartbeating;
			spin_unlock_irqrestore(&reg->hr_lock, flags);
			break;
		}
	}
	spin_unlock(&o2hb_live_lock);

	return num;
}
EXPORT_SYMBOL_GPL(o2hb_get_livenode_nr);

/*
 * get a map of all nodes that are heartbeating in any regions
 */
void o2hb_fill_node_map(unsigned long *map, unsigned int bytes, const char *uuid)
{
	/* callers want to serialize this map and callbacks so that they
	 * can trust that they don't miss nodes coming to the party */
	down_read(&o2hb_callback_sem);
	o2hb_fill_node_map_from_callback(map, bytes, uuid);
	up_read(&o2hb_callback_sem);
}
EXPORT_SYMBOL_GPL(o2hb_fill_node_map);

/*
 * heartbeat configfs bits.  The heartbeat set is a default set under
 * the cluster set in nodemanager.c.
 */

static struct o2hb_region *to_o2hb_region(struct config_item *item)
{
	return item ? container_of(item, struct o2hb_region, hr_item) : NULL;
}

/* drop_item only drops its ref after killing the thread, nothing should
 * be using the region anymore.  this has to clean up any state that
 * attributes might have built up. */
static void o2hb_region_release(struct config_item *item)
{
	int i;
	struct page *page;
	struct o2hb_region *reg = to_o2hb_region(item);

	kfree(reg->hr_tmp_block);

	if (reg->hr_slot_data) {
		for (i = 0; i < reg->hr_num_pages; i++) {
			page = reg->hr_slot_data[i];
			if (page)
				__free_page(page);
		}
		kfree(reg->hr_slot_data);
	}

	if (reg->hr_bdev)
		blkdev_put(reg->hr_bdev, FMODE_READ|FMODE_WRITE);

	if (reg->hr_slots) {
		for (i = 0; i < reg->hr_blocks; i++)
			kfree(reg->hr_slots[i]);
		kfree(reg->hr_slots);
	}

	for (i = 0; i < O2NM_MAX_NODES; i++)
		kfree(reg->hr_conn_failure_bm[i]);

	debugfs_remove(reg->hr_debug_livenodes);
	debugfs_remove(reg->hr_debug_regnum);
	debugfs_remove(reg->hr_debug_elapsed_time);
	debugfs_remove(reg->hr_debug_pinned);
	debugfs_remove(reg->hr_debug_inflight_hb_io);
	debugfs_remove(reg->hr_debug_dir);
	debugfs_remove(reg->hr_debug_o2state);
	kfree(reg->hr_db_regnum);
	kfree(reg->hr_db_livenodes);
	kfree(reg->hr_db_elapsed_time);
	kfree(reg->hr_db_pinned);
	kfree(reg->hr_db_inflight_hb_io);

	spin_lock(&o2hb_live_lock);
	list_del(&reg->hr_all_item);
	spin_unlock(&o2hb_live_lock);

	kfree(reg);
}

static int o2hb_read_block_input(struct o2hb_region *reg,
				 const char *page,
				 unsigned long *ret_bytes,
				 unsigned int *ret_bits)
{
	unsigned long bytes;
	char *p = (char *)page;

	bytes = simple_strtoul(p, &p, 0);
	if (!p || (*p && (*p != '\n')))
		return -EINVAL;

	/* Heartbeat and fs min / max block sizes are the same. */
	if (bytes > 4096 || bytes < 512)
		return -ERANGE;
	if (hweight16(bytes) != 1)
		return -EINVAL;

	if (ret_bytes)
		*ret_bytes = bytes;
	if (ret_bits)
		*ret_bits = ffs(bytes) - 1;

	return 0;
}

static ssize_t o2hb_region_block_bytes_show(struct config_item *item,
					    char *page)
{
	return sprintf(page, "%u\n", to_o2hb_region(item)->hr_block_bytes);
}

static ssize_t o2hb_region_block_bytes_store(struct config_item *item,
					     const char *page,
					     size_t count)
{
	struct o2hb_region *reg = to_o2hb_region(item);
	int status;
	unsigned long block_bytes;
	unsigned int block_bits;

	if (reg->hr_bdev)
		return -EINVAL;

	status = o2hb_read_block_input(reg, page, &block_bytes,
				       &block_bits);
	if (status)
		return status;

	reg->hr_block_bytes = (unsigned int)block_bytes;
	reg->hr_block_bits = block_bits;

	return count;
}

static ssize_t o2hb_region_start_block_show(struct config_item *item,
					    char *page)
{
	return sprintf(page, "%llu\n", to_o2hb_region(item)->hr_start_block);
}

static ssize_t o2hb_region_start_block_store(struct config_item *item,
					     const char *page,
					     size_t count)
{
	struct o2hb_region *reg = to_o2hb_region(item);
	unsigned long long tmp;
	char *p = (char *)page;

	if (reg->hr_bdev)
		return -EINVAL;

	tmp = simple_strtoull(p, &p, 0);
	if (!p || (*p && (*p != '\n')))
		return -EINVAL;

	reg->hr_start_block = tmp;

	return count;
}

static ssize_t o2hb_region_blocks_show(struct config_item *item, char *page)
{
	return sprintf(page, "%d\n", to_o2hb_region(item)->hr_blocks);
}

static ssize_t o2hb_region_blocks_store(struct config_item *item,
					const char *page,
					size_t count)
{
	struct o2hb_region *reg = to_o2hb_region(item);
	unsigned long tmp;
	char *p = (char *)page;

	if (reg->hr_bdev)
		return -EINVAL;

	tmp = simple_strtoul(p, &p, 0);
	if (!p || (*p && (*p != '\n')))
		return -EINVAL;

	if (tmp > O2NM_MAX_NODES || tmp == 0)
		return -ERANGE;

	reg->hr_blocks = (unsigned int)tmp;

	return count;
}

static ssize_t o2hb_region_dev_show(struct config_item *item, char *page)
{
	unsigned int ret = 0;

	if (to_o2hb_region(item)->hr_bdev)
		ret = sprintf(page, "%s\n", to_o2hb_region(item)->hr_dev_name);

	return ret;
}

static void o2hb_init_region_params(struct o2hb_region *reg)
{
	reg->hr_slots_per_page = PAGE_SIZE >> reg->hr_block_bits;
	reg->hr_timeout_ms = O2HB_REGION_TIMEOUT_MS;
	INIT_LIST_HEAD(&reg->hr_inflight_io);

	mlog(ML_HEARTBEAT, "hr_start_block = %llu, hr_blocks = %u\n",
	     reg->hr_start_block, reg->hr_blocks);
	mlog(ML_HEARTBEAT, "hr_block_bytes = %u, hr_block_bits = %u\n",
	     reg->hr_block_bytes, reg->hr_block_bits);
	mlog(ML_HEARTBEAT, "hr_timeout_ms = %u\n", reg->hr_timeout_ms);
	mlog(ML_HEARTBEAT, "dead threshold = %u\n", o2hb_real_dead_threshold);
}

static int o2hb_map_slot_data(struct o2hb_region *reg)
{
	int i, j;
	unsigned int last_slot;
	unsigned int spp = reg->hr_slots_per_page;
	struct page *page;
	char *raw;
	struct o2hb_disk_slot *slot;
	char wq_name[O2NM_MAX_NAME_LEN];

	reg->hr_tmp_block = kmalloc(reg->hr_block_bytes, GFP_KERNEL);
	if (reg->hr_tmp_block == NULL)
		return -ENOMEM;

	/* fix: malloc each o2hb_disk_slot separately
	 * to avoid big continuous memory allocation */
	reg->hr_slots = kcalloc(reg->hr_blocks, sizeof(struct o2hb_disk_slot *),
			GFP_KERNEL);
	if (!reg->hr_slots)
		return -ENOMEM;

	for (i = 0; i < reg->hr_blocks; i++) {
		reg->hr_slots[i] = kzalloc(sizeof(struct o2hb_disk_slot), GFP_KERNEL);
		if (!reg->hr_slots[i])
			return -ENOMEM;
	}

	snprintf(wq_name, O2NM_MAX_NAME_LEN, "o2hb_wq-%s",
			reg->hr_item.ci_name);
	reg->o2hb_worker = alloc_ordered_workqueue(wq_name, WQ_MEM_RECLAIM);
	if (reg->o2hb_worker == NULL)
		return -ENOMEM;

	for(i = 0; i < reg->hr_blocks; i++) {
		slot = reg->hr_slots[i];
		slot->ds_node_num = i;
		INIT_LIST_HEAD(&slot->ds_live_item);
		slot->ds_raw_block = NULL;
		slot->ds_fake_timeout = 0;
		slot->conn_query_done = 0;
		slot->ds_quorum_fence_samples = 0;
		spin_lock_init(&slot->ds_lock);
		INIT_WORK(&slot->conn_failure_query, o2hb_conn_failure_query);
		INIT_WORK(&slot->ds_pre_check_node, o2hb_pre_check_node);
		slot->reg = reg;
	}

	reg->hr_num_pages = (reg->hr_blocks + spp - 1) / spp;
	mlog(ML_HEARTBEAT, "Going to require %u pages to cover %u blocks "
			   "at %u blocks per page\n",
	     reg->hr_num_pages, reg->hr_blocks, spp);

	reg->hr_slot_data = kcalloc(reg->hr_num_pages, sizeof(struct page *),
				    GFP_KERNEL);
	if (!reg->hr_slot_data)
		return -ENOMEM;

	for(i = 0; i < reg->hr_num_pages; i++) {
		page = alloc_page(GFP_KERNEL);
		if (!page)
			return -ENOMEM;

		reg->hr_slot_data[i] = page;

		last_slot = i * spp;
		raw = page_address(page);
		for (j = 0;
		     (j < spp) && ((j + last_slot) < reg->hr_blocks);
		     j++) {
			BUG_ON((j + last_slot) >= reg->hr_blocks);

			slot = reg->hr_slots[j + last_slot];
			slot->ds_raw_block =
				(struct o2hb_disk_heartbeat_block *) raw;

			raw += reg->hr_block_bytes;
		}
	}

	return 0;
}

/* Read in all the slots available and populate the tracking
 * structures so that we can start with a baseline idea of what's
 * there. */
static int o2hb_populate_slot_data(struct o2hb_region *reg)
{
	int ret, i;
	struct o2hb_disk_slot *slot;
	struct o2hb_disk_heartbeat_block *hb_block;

	ret = o2hb_read_slots(reg, 0, reg->hr_blocks);
	if (ret)
		goto out;

	/* We only want to get an idea of the values initially in each
	 * slot, so we do no verification - o2hb_check_slot will
	 * actually determine if each configured slot is valid and
	 * whether any values have changed. */
	for(i = 0; i < reg->hr_blocks; i++) {
		slot = reg->hr_slots[i];
		hb_block = (struct o2hb_disk_heartbeat_block *) slot->ds_raw_block;

		/* Only fill the values that o2hb_check_slot uses to
		 * determine changing slots */
		slot->ds_last_time = le64_to_cpu(hb_block->hb_seq);
		slot->ds_last_generation = le64_to_cpu(hb_block->hb_generation);
	}

out:
	return ret;
}

/* we should flush pre_check work before joining domain, in case of
 * next mount thread queue pre_check work conflicting with this pre_check work */
static void o2hb_cancel_pre_check_work(struct o2hb_region *reg)
{
	int i;
	struct o2hb_disk_slot *slot;

	mlog(ML_NOTICE, "%s: Cancel o2hb_pre_check work.\n",
			config_item_name(&reg->hr_item));
	for (i = 0; i < reg->hr_blocks; i++) {
		slot = reg->hr_slots[i];
		cancel_work_sync(&slot->ds_pre_check_node);
	}
}

/* this is acting as commit; we set up all of hr_bdev and hr_task or nothing */
static ssize_t o2hb_region_dev_store(struct config_item *item,
				     const char *page,
				     size_t count)
{
	struct o2hb_region *reg = to_o2hb_region(item);
	struct task_struct *hb_task, *hb_write_task;
	long fd;
	int sectsize;
	char *p = (char *)page;
	struct file *filp = NULL;
	struct inode *inode = NULL;
	ssize_t ret = -EINVAL;
	int live_threshold, i;
	unsigned long flags;
	struct workqueue_struct *tmp_o2hb_worker;

	if (reg->hr_bdev)
		goto out;

	/* We can't heartbeat without having had our node number
	 * configured yet. */
	if (o2nm_this_node() == O2NM_MAX_NODES)
		goto out;

	fd = simple_strtol(p, &p, 0);
	if (!p || (*p && (*p != '\n')))
		goto out;

	if (fd < 0 || fd >= INT_MAX)
		goto out;

	filp = fget(fd);
	if (filp == NULL)
		goto out;

	if (reg->hr_blocks == 0 || reg->hr_start_block == 0 ||
	    reg->hr_block_bytes == 0)
		goto out;

	inode = igrab(filp->f_mapping->host);
	if (inode == NULL)
		goto out;

	if (!S_ISBLK(inode->i_mode))
		goto out;

	reg->hr_bdev = blkdev_get_by_dev(filp->f_mapping->host->i_rdev,
							FMODE_WRITE | FMODE_READ, NULL);
	if (IS_ERR(reg->hr_bdev)) {
		ret = PTR_ERR(reg->hr_bdev);
		reg->hr_bdev = NULL;
		goto out;
	}
	inode = NULL;

	bdevname(reg->hr_bdev, reg->hr_dev_name);

	sectsize = bdev_logical_block_size(reg->hr_bdev);
	if (sectsize != reg->hr_block_bytes) {
		mlog(ML_ERROR,
		     "%s: blocksize %u incorrect for device, expected %d\n",
		     config_item_name(&reg->hr_item), reg->hr_block_bytes, sectsize);
		ret = -EINVAL;
		goto out;
	}

	o2hb_init_region_params(reg);

	/* Generation of zero is invalid */
	/* generation 1 stands for quorum fence */
	do {
		get_random_bytes(&reg->hr_generation,
				 sizeof(reg->hr_generation));
	} while (reg->hr_generation == 0 || reg->hr_generation == 1 ||
			reg->hr_generation == 2);

	ret = o2hb_map_slot_data(reg);
	if (ret) {
		mlog_errno(ret);
		goto out;
	}

	ret = o2hb_populate_slot_data(reg);
	if (ret) {
		mlog_errno(ret);
		goto out;
	}

	/* since o2hb_read_slots in o2hb_populate_slot_data and
	 * here may return the same, we delay 2s to let hb_block
	 * update */
	msleep(2000);
	ret = o2hb_read_slots(reg, 0, reg->hr_blocks);
	if (ret) {
		mlog_errno(ret);
		goto out;
	}

	/* check if multi cluster situation */
	for (i = 0; i < reg->hr_blocks; ++i) {
		if (o2hb_check_slot_multicluster(reg, reg->hr_slots[i], NULL)) {
			/* multi clusters situation, quit */
			mlog(ML_ERROR, "%s: multi-cluster happened!\n",
					config_item_name(&reg->hr_item));
			ret = -EPERM;
			goto out;
		}
	}

	INIT_DELAYED_WORK(&reg->hr_write_fake_timeout_work,
			o2hb_write_fake_timeout);
	INIT_DELAYED_WORK(&reg->hr_write_real_timeout_work,
			o2hb_write_real_timeout);

	/*
	 * A node is considered live after it has beat LIVE_THRESHOLD
	 * times.  We're not steady until we've given them a chance
	 * _after_ our first read.
	 * The default threshold is bare minimum so as to limit the delay
	 * during mounts. For global heartbeat, the threshold doubled for the
	 * first region.
	 */
	live_threshold = O2HB_LIVE_THRESHOLD;
	if (o2hb_global_heartbeat_active()) {
		spin_lock(&o2hb_live_lock);
		if (bitmap_weight(o2hb_region_bitmap, O2NM_MAX_REGIONS) == 1)
			live_threshold <<= 1;
		spin_unlock(&o2hb_live_lock);
	}
	atomic_set(&reg->hr_steady_iterations, live_threshold + 1);
	atomic_set(&reg->hr_own_slot_populated, 0);
	atomic_set(&reg->hr_write_hb_task_exited, 0);

	hb_write_task = kthread_create(o2hb_write_thread, reg, "o2hb_write-%s",
			      reg->hr_item.ci_name);
	if (IS_ERR(hb_write_task)) {
		ret = PTR_ERR(hb_write_task);
		mlog_errno(ret);
		goto out;
	}
	get_task_struct(hb_write_task);
	wake_up_process(hb_write_task);
	spin_lock_irqsave(&reg->hr_lock, flags);
	reg->hr_write_task = hb_write_task;
	spin_unlock_irqrestore(&reg->hr_lock, flags);

	ret = wait_event_interruptible(o2hb_write_thread_queue,
			atomic_read(&reg->hr_own_slot_populated) ||
			atomic_read(&reg->hr_write_hb_task_exited));
	if (ret || atomic_read(&reg->hr_write_hb_task_exited)) {
		spin_lock_irqsave(&reg->hr_lock, flags);
		hb_write_task = reg->hr_write_task;
		reg->hr_write_task = NULL;
		spin_unlock_irqrestore(&reg->hr_lock, flags);

		if (hb_write_task) {
			kthread_stop(hb_write_task);
			put_task_struct(hb_write_task);
		}
		goto out;
	}

	hb_task = kthread_create(o2hb_thread, reg, "o2hb-%s",
			      reg->hr_item.ci_name);
	if (IS_ERR(hb_task)) {
		spin_lock_irqsave(&reg->hr_lock, flags);
		hb_write_task = reg->hr_write_task;
		reg->hr_write_task = NULL;
		spin_unlock_irqrestore(&reg->hr_lock, flags);

		if (hb_write_task) {
			kthread_stop(hb_write_task);
			put_task_struct(hb_write_task);
		}

		ret = PTR_ERR(hb_task);
		mlog_errno(ret);
		goto out;
	}
	/* get reference to prevent from NULL pointer dereference
	 * when umount thread stop hr_task */
	get_task_struct(hb_task);
	wake_up_process(hb_task);
	spin_lock_irqsave(&reg->hr_lock, flags);
	reg->hr_task = hb_task;
	spin_unlock_irqrestore(&reg->hr_lock, flags);

	ret = wait_event_interruptible(o2hb_steady_queue,
				atomic_read(&reg->hr_steady_iterations) == 0 ||
				reg->hr_disk_timeout ||
				reg->hr_inconsistency ||
				reg->hr_ltype_inconsistency ||
				reg->hr_multi_cluster);

	o2hb_cancel_pre_check_work(reg);

	if (ret || reg->hr_disk_timeout || reg->hr_inconsistency ||
			reg->hr_ltype_inconsistency || reg->hr_multi_cluster) {
		/* We got interrupted (hello ptrace!).  Clean up */
		spin_lock_irqsave(&reg->hr_lock, flags);
		hb_task = reg->hr_task;
		reg->hr_task = NULL;

		hb_write_task = reg->hr_write_task;
		reg->hr_write_task = NULL;
		spin_unlock_irqrestore(&reg->hr_lock, flags);

		if (hb_write_task) {
			kthread_stop(hb_write_task);
			put_task_struct(hb_write_task);
		}

		if (hb_task) {
			kthread_stop(hb_task);
			/* put the last reference and release hb_task */
			put_task_struct(hb_task);
		}
		goto out;
	}

	/* Ok, we were woken.  Make sure it wasn't by drop_item() */
	spin_lock(&o2hb_live_lock);
	if (o2hb_global_heartbeat_active())
		set_bit(reg->hr_region_num, o2hb_live_region_bitmap);
	spin_unlock(&o2hb_live_lock);

	spin_lock_irqsave(&reg->hr_lock, flags);
	hb_task = reg->hr_task;
	spin_unlock_irqrestore(&reg->hr_lock, flags);
	if (hb_task)
		ret = count;
	else
		ret = -EIO;

	if (hb_task && o2hb_global_heartbeat_active())
		mlog(ML_NOTICE, "Heartbeat started on region %s\n",
			config_item_name(&reg->hr_item));

out:
	if (filp)
		fput(filp);
	iput(inode);

	if (ret < 0 || reg->hr_disk_timeout || reg->hr_node_deleted ||
			reg->hr_inconsistency || reg->hr_ltype_inconsistency) {
		/* sync with o2hb_thread */
		spin_lock_irqsave(&reg->hr_lock, flags);
		tmp_o2hb_worker = reg->o2hb_worker;
		reg->o2hb_worker = NULL;
		spin_unlock_irqrestore(&reg->hr_lock, flags);
		if (tmp_o2hb_worker)
			destroy_workqueue(tmp_o2hb_worker);
		if (reg->hr_bdev) {
			blkdev_put(reg->hr_bdev, FMODE_READ|FMODE_WRITE);
			reg->hr_bdev = NULL;
		}
	}

	if (reg->hr_multi_cluster)
		ret = -EPERM;
	if (reg->hr_node_deleted || reg->hr_ltype_inconsistency)
		ret = -EINVAL;
	else if (reg->hr_disk_timeout)
		ret = -EIO;
	/* Use EREMOTEIO to distinguish situations where other nodes in the cluster
	 * can't get my heartbeat */
	else if (reg->hr_inconsistency)
		ret = -EREMOTEIO;

	return ret;
}

static ssize_t o2hb_region_pid_show(struct config_item *item, char *page)
{
	struct o2hb_region *reg = to_o2hb_region(item);
	pid_t pid = 0;
	unsigned long flags;

	spin_lock_irqsave(&reg->hr_lock, flags);
	if (reg->hr_task)
		pid = task_pid_nr(reg->hr_task);
	spin_unlock_irqrestore(&reg->hr_lock, flags);

	if (!pid)
		return 0;

	return sprintf(page, "%u\n", pid);
}

static ssize_t o2hb_region_write_pid_show(struct config_item *item, char *page)
{
	struct o2hb_region *reg = to_o2hb_region(item);
	pid_t pid = 0;
	unsigned long flags;

	spin_lock_irqsave(&reg->hr_lock, flags);
	if (reg->hr_write_task)
		pid = task_pid_nr(reg->hr_write_task);
	spin_unlock_irqrestore(&reg->hr_lock, flags);

	if (!pid)
		return 0;

	return sprintf(page, "%u\n", pid);
}

#define O2HB_LOCK_TYPE_DLM	"dlm"
#define O2HB_LOCK_TYPE_ADL	"adl"
#define O2HB_LOCK_TYPE_LEN	3
static ssize_t o2hb_region_lock_type_show(struct config_item *item, char *page)

{
	return sprintf(page, "%s\n",
			to_o2hb_region(item)->lock_type_adl ?
				O2HB_LOCK_TYPE_ADL : O2HB_LOCK_TYPE_DLM);
}

static ssize_t o2hb_region_lock_type_store(struct config_item *item,
					const char *page,
					size_t count)
{
	struct o2hb_region *reg = to_o2hb_region(item);

	if (reg->hr_bdev)
		return -EINVAL;

	if (strlen(page) != count)
		return -EINVAL;

	if (!strncmp(page, O2HB_LOCK_TYPE_DLM, O2HB_LOCK_TYPE_LEN + 1))
		reg->lock_type_adl = 0;
	else if (!strncmp(page, O2HB_LOCK_TYPE_ADL, O2HB_LOCK_TYPE_LEN + 1))
		reg->lock_type_adl = 1;
	else
		return -EINVAL;

	return count;
}

static ssize_t o2hb_region_fence_show(struct config_item *item, char *page)
{
	return sprintf(page, "%s\n", to_o2hb_region(item)->mount_with_fence ? "on" : "off");
}

static ssize_t o2hb_region_fence_store(struct config_item *item,
					const char *page,
					size_t count)
{
	struct o2hb_region *reg = to_o2hb_region(item);

	if (strlen(page) != count)
		return -EINVAL;

	if (!strncmp(page, "on", strlen("on") + 1))
		reg->mount_with_fence = true;
	else if (!strncmp(page, "off", strlen("off") + 1))
		reg->mount_with_fence = false;
	else
		return -EINVAL;

	return count;
}

CONFIGFS_ATTR(o2hb_region_, block_bytes);
CONFIGFS_ATTR(o2hb_region_, start_block);
CONFIGFS_ATTR(o2hb_region_, blocks);
CONFIGFS_ATTR(o2hb_region_, dev);
CONFIGFS_ATTR_RO(o2hb_region_, pid);
CONFIGFS_ATTR_RO(o2hb_region_, write_pid);
CONFIGFS_ATTR(o2hb_region_, lock_type);
CONFIGFS_ATTR(o2hb_region_, fence);

static struct configfs_attribute *o2hb_region_attrs[] = {
	&o2hb_region_attr_block_bytes,
	&o2hb_region_attr_start_block,
	&o2hb_region_attr_blocks,
	&o2hb_region_attr_dev,
	&o2hb_region_attr_pid,
	&o2hb_region_attr_write_pid,
	&o2hb_region_attr_lock_type,
	&o2hb_region_attr_fence,
	NULL,
};

static struct configfs_item_operations o2hb_region_item_ops = {
	.release		= o2hb_region_release,
};

static const struct config_item_type o2hb_region_type = {
	.ct_item_ops	= &o2hb_region_item_ops,
	.ct_attrs	= o2hb_region_attrs,
	.ct_owner	= THIS_MODULE,
};

/* heartbeat set */

struct o2hb_heartbeat_group {
	struct config_group hs_group;
	/* some stuff? */
};

static struct o2hb_heartbeat_group *to_o2hb_heartbeat_group(struct config_group *group)
{
	return group ?
		container_of(group, struct o2hb_heartbeat_group, hs_group)
		: NULL;
}

static int o2hb_debug_region_init(struct o2hb_region *reg, struct dentry *dir)
{
	int ret = -ENOMEM;

	reg->hr_debug_dir =
		debugfs_create_dir(config_item_name(&reg->hr_item), dir);
	if (!reg->hr_debug_dir) {
		mlog_errno(ret);
		goto bail;
	}

	reg->hr_debug_o2state =
			debugfs_create_file(config_item_name(&reg->hr_item),
				S_IFREG|S_IRUSR, o2state_debug_dir, reg, &o2state_debug_fops);
	if (!reg->hr_debug_o2state) {
		mlog_errno(ret);
		goto bail;
	}

	reg->hr_debug_livenodes =
			o2hb_debug_create(O2HB_DEBUG_LIVENODES,
					  reg->hr_debug_dir,
					  &(reg->hr_db_livenodes),
					  sizeof(*(reg->hr_db_livenodes)),
					  O2HB_DB_TYPE_REGION_LIVENODES,
					  sizeof(reg->hr_live_node_bitmap),
					  O2NM_MAX_NODES, reg);
	if (!reg->hr_debug_livenodes) {
		mlog_errno(ret);
		goto bail;
	}

	reg->hr_debug_regnum =
			o2hb_debug_create(O2HB_DEBUG_REGION_NUMBER,
					  reg->hr_debug_dir,
					  &(reg->hr_db_regnum),
					  sizeof(*(reg->hr_db_regnum)),
					  O2HB_DB_TYPE_REGION_NUMBER,
					  0, O2NM_MAX_NODES, reg);
	if (!reg->hr_debug_regnum) {
		mlog_errno(ret);
		goto bail;
	}

	reg->hr_debug_elapsed_time =
			o2hb_debug_create(O2HB_DEBUG_REGION_ELAPSED_TIME,
					  reg->hr_debug_dir,
					  &(reg->hr_db_elapsed_time),
					  sizeof(*(reg->hr_db_elapsed_time)),
					  O2HB_DB_TYPE_REGION_ELAPSED_TIME,
					  0, 0, reg);
	if (!reg->hr_debug_elapsed_time) {
		mlog_errno(ret);
		goto bail;
	}

	reg->hr_debug_pinned =
			o2hb_debug_create(O2HB_DEBUG_REGION_PINNED,
					  reg->hr_debug_dir,
					  &(reg->hr_db_pinned),
					  sizeof(*(reg->hr_db_pinned)),
					  O2HB_DB_TYPE_REGION_PINNED,
					  0, 0, reg);
	if (!reg->hr_debug_pinned) {
		mlog_errno(ret);
		goto bail;
	}

	reg->hr_debug_inflight_hb_io =
			o2hb_debug_create(O2HB_DEBUG_INFLIGHT_HB_IO,
					  reg->hr_debug_dir,
					  &(reg->hr_db_inflight_hb_io),
					  sizeof(*(reg->hr_db_inflight_hb_io)),
					  O2HB_DB_TYPE_REGION_INFLIGHT_HB_IO,
					  0, 0, reg);
	if (!reg->hr_debug_inflight_hb_io) {
		mlog_errno(ret);
		goto bail;
	}

	ret = 0;
bail:
	return ret;
}

static struct config_item *o2hb_heartbeat_group_make_item(struct config_group *group,
							  const char *name)
{
	struct o2hb_region *reg = NULL;
	int ret, i;

	reg = kzalloc(sizeof(struct o2hb_region), GFP_KERNEL);
	if (reg == NULL)
		return ERR_PTR(-ENOMEM);

	for (i = 0; i < O2NM_MAX_NODES; i++) {
		reg->hr_conn_failure_bm[i] =
			kcalloc(BITS_TO_LONGS(O2NM_MAX_NODES), sizeof(unsigned long), GFP_KERNEL);
		if (!reg->hr_conn_failure_bm[i]) {
			ret = -ENOMEM;
			goto free;
		}
	}

	if (strlen(name) > O2HB_MAX_REGION_NAME_LEN) {
		ret = -ENAMETOOLONG;
		goto free;
	}

	spin_lock_init(&reg->hr_lock);

	spin_lock(&o2hb_live_lock);
	reg->hr_region_num = 0;
	reg->key = crc32_le(0, name, strlen(name));
	if (o2hb_global_heartbeat_active()) {
		reg->hr_region_num = find_first_zero_bit(o2hb_region_bitmap,
							 O2NM_MAX_REGIONS);
		if (reg->hr_region_num >= O2NM_MAX_REGIONS) {
			spin_unlock(&o2hb_live_lock);
			ret = -EFBIG;
			goto free;
		}
		set_bit(reg->hr_region_num, o2hb_region_bitmap);
	}
	list_add_tail(&reg->hr_all_item, &o2hb_all_regions);
	spin_unlock(&o2hb_live_lock);

	config_item_init_type_name(&reg->hr_item, name, &o2hb_region_type);

	ret = o2hb_debug_region_init(reg, o2hb_debug_dir);
	if (ret) {
		config_item_put(&reg->hr_item);
		return ERR_PTR(ret);
	}

	return &reg->hr_item;
free:
	for (i = 0; i < O2NM_MAX_NODES; i++) {
		kfree(reg->hr_conn_failure_bm[i]);
		reg->hr_conn_failure_bm[i] = NULL;
	}
	kfree(reg);
	return ERR_PTR(ret);
}

static void o2hb_heartbeat_group_drop_item(struct config_group *group,
					   struct config_item *item)
{
	struct task_struct *hb_task, *hb_write_task;
	struct o2hb_region *reg = to_o2hb_region(item);
	int quorum_region = 0;
	unsigned long flags;
	u16 node_num;
	struct o2hb_disk_slot *slot;
	struct o2nm_node *node;
	struct o2hb_node_event event = {
		.hn_item = LIST_HEAD_INIT(event.hn_item),
	};
	int queued = 0;

	/* stop the thread when the user removes the region dir */
	spin_lock(&o2hb_live_lock);
	if (o2hb_global_heartbeat_active()) {
		clear_bit(reg->hr_region_num, o2hb_region_bitmap);
		clear_bit(reg->hr_region_num, o2hb_live_region_bitmap);
		if (test_bit(reg->hr_region_num, o2hb_quorum_region_bitmap))
			quorum_region = 1;
		clear_bit(reg->hr_region_num, o2hb_quorum_region_bitmap);
	}

	spin_lock_irqsave(&reg->hr_lock, flags);
	hb_write_task = reg->hr_write_task;
	reg->hr_write_task = NULL;

	hb_task = reg->hr_task;
	reg->hr_task = NULL;
	reg->hr_item_dropped = 1;
	spin_unlock_irqrestore(&reg->hr_lock, flags);
	spin_unlock(&o2hb_live_lock);

	if (hb_write_task) {
		kthread_stop(hb_write_task);
		put_task_struct(hb_write_task);
	}

	if (hb_task) {
		kthread_stop(hb_task);
		/* put the last reference and release hb_task */
		put_task_struct(hb_task);
	}

	if (!reg->hr_unclean_stop)
		goto skip_shutdown_own;

	node_num = o2nm_this_node();
	node = o2nm_get_node_by_num(node_num);
	if (!node)
		goto skip_shutdown_own;
	slot = reg->hr_slots[node_num];
	spin_lock(&o2hb_live_lock);
	if (!list_empty(&slot->ds_live_item)) {
		list_del_init(&slot->ds_live_item);
		if (list_empty(&o2hb_live_slots[node_num])) {
			clear_bit(slot->ds_node_num, o2hb_live_node_bitmap);
			mlog(ML_NOTICE,
					"Remove node %d from live nodes bitmap, uuid: %s\n",
					slot->ds_node_num,
					config_item_name(&reg->hr_item));
			o2hb_queue_node_event(&event, O2HB_NODE_DOWN_CB, node,
					slot->ds_node_num, config_item_name(&reg->hr_item),
					reg->hr_generation);
			queued = 1;
		}
		o2quo_hb_down(slot->ds_node_num, reg->lock_type_adl);
	}

	spin_unlock(&o2hb_live_lock);
	if (queued)
		o2hb_run_event_list(&event);

	o2nm_node_put(node);
	o2nm_undepend_this_node();

skip_shutdown_own:
	/*
	 * If we're racing a dev_write(), we need to wake them.  They will
	 * check reg->hr_task
	 */
	if (atomic_read(&reg->hr_steady_iterations) != 0) {
		atomic_set(&reg->hr_steady_iterations, 0);
		wake_up(&o2hb_steady_queue);
	}

	if (o2hb_global_heartbeat_active())
		mlog(ML_NOTICE, "Heartbeat stopped on region %s\n",
		       config_item_name(&reg->hr_item));

	config_item_put(item);

	if (!o2hb_global_heartbeat_active() || !quorum_region)
		return;

	/*
	 * If global heartbeat active and there are dependent users,
	 * pin all regions if quorum region count <= CUT_OFF
	 */
	spin_lock(&o2hb_live_lock);

	if (!o2hb_dependent_users)
		goto unlock;

	if (bitmap_weight(o2hb_quorum_region_bitmap,
			   O2NM_MAX_REGIONS) <= O2HB_PIN_CUT_OFF)
		o2hb_region_pin(NULL);

unlock:
	spin_unlock(&o2hb_live_lock);
}

static ssize_t o2hb_heartbeat_group_fake_dead_threshold_show(
		struct config_item *item, char *page)
{
	return sprintf(page, "%u\n", o2hb_fake_dead_threshold);
}

static ssize_t o2hb_heartbeat_group_fake_dead_threshold_store(
		struct config_item *item,
		const char *page, size_t count)
{
	unsigned long tmp;
	char *p = (char *)page;

	tmp = simple_strtoul(p, &p, 10);
	if (!p || (*p && (*p != '\n')))
		return -EINVAL;

	/* this will validate ranges for us. */
	o2hb_fake_dead_threshold_set((unsigned int) tmp);

	return count;
}

static ssize_t o2hb_heartbeat_group_real_dead_threshold_show(
		struct config_item *item, char *page)
{
	return sprintf(page, "%u\n", o2hb_real_dead_threshold);
}

static ssize_t o2hb_heartbeat_group_real_dead_threshold_store(
		struct config_item *item,
		const char *page, size_t count)
{
	unsigned long tmp;
	char *p = (char *)page;

	tmp = simple_strtoul(p, &p, 10);
	if (!p || (*p && (*p != '\n')))
		return -EINVAL;

	/* this will validate ranges for us. */
	o2hb_real_dead_threshold_set((unsigned int) tmp);

	return count;
}

static ssize_t o2hb_heartbeat_group_mode_show(struct config_item *item,
		char *page)
{
	return sprintf(page, "%s\n",
		       o2hb_heartbeat_mode_desc[o2hb_heartbeat_mode]);
}

static ssize_t o2hb_heartbeat_group_mode_store(struct config_item *item,
		const char *page, size_t count)
{
	unsigned int i;
	int ret;
	size_t len;

	len = (page[count - 1] == '\n') ? count - 1 : count;
	if (!len)
		return -EINVAL;

	for (i = 0; i < O2HB_HEARTBEAT_NUM_MODES; ++i) {
		if (strncasecmp(page, o2hb_heartbeat_mode_desc[i], len))
			continue;

		ret = o2hb_global_heartbeat_mode_set(i);
		if (!ret)
			printk(KERN_NOTICE "o2hb: Heartbeat mode set to %s\n",
			       o2hb_heartbeat_mode_desc[i]);
		return count;
	}

	return -EINVAL;

}

CONFIGFS_ATTR(o2hb_heartbeat_group_, fake_dead_threshold);
CONFIGFS_ATTR(o2hb_heartbeat_group_, real_dead_threshold);
CONFIGFS_ATTR(o2hb_heartbeat_group_, mode);

static struct configfs_attribute *o2hb_heartbeat_group_attrs[] = {
	&o2hb_heartbeat_group_attr_fake_dead_threshold,
	&o2hb_heartbeat_group_attr_real_dead_threshold,
	&o2hb_heartbeat_group_attr_mode,
	NULL,
};

static struct configfs_group_operations o2hb_heartbeat_group_group_ops = {
	.make_item	= o2hb_heartbeat_group_make_item,
	.drop_item	= o2hb_heartbeat_group_drop_item,
};

static const struct config_item_type o2hb_heartbeat_group_type = {
	.ct_group_ops	= &o2hb_heartbeat_group_group_ops,
	.ct_attrs	= o2hb_heartbeat_group_attrs,
	.ct_owner	= THIS_MODULE,
};

/* this is just here to avoid touching group in heartbeat.h which the
 * entire damn world #includes */
struct config_group *o2hb_alloc_hb_set(void)
{
	struct o2hb_heartbeat_group *hs = NULL;
	struct config_group *ret = NULL;

	hs = kzalloc(sizeof(struct o2hb_heartbeat_group), GFP_KERNEL);
	if (hs == NULL)
		goto out;

	config_group_init_type_name(&hs->hs_group, "heartbeat",
				    &o2hb_heartbeat_group_type);

	ret = &hs->hs_group;
out:
	if (ret == NULL)
		kfree(hs);
	return ret;
}

void o2hb_free_hb_set(struct config_group *group)
{
	struct o2hb_heartbeat_group *hs = to_o2hb_heartbeat_group(group);
	kfree(hs);
}

/* hb callback registration and issuing */

static struct o2hb_callback *hbcall_from_type(enum o2hb_callback_type type)
{
	if (type == O2HB_NUM_CB)
		return ERR_PTR(-EINVAL);

	return &o2hb_callbacks[type];
}

void o2hb_setup_callback(struct o2hb_callback_func *hc,
			 enum o2hb_callback_type type,
			 o2hb_cb_func *func,
			 void *data,
			 int priority,
			 const char *uuid)
{
	INIT_LIST_HEAD(&hc->hc_item);
	hc->hc_func = func;
	hc->hc_data = data;
	hc->hc_priority = priority;
	hc->hc_type = type;
	hc->hc_magic = O2HB_CB_MAGIC;
	memset(hc->region_uuid, 0, O2HB_REGION_UUID_LEN + 1);
	if (uuid)
		memcpy(hc->region_uuid, uuid, O2HB_REGION_UUID_LEN);
}
EXPORT_SYMBOL_GPL(o2hb_setup_callback);

/*
 * In local heartbeat mode, region_uuid passed matches the dlm domain name.
 * In global heartbeat mode, region_uuid passed is NULL.
 *
 * In local, we only pin the matching region. In global we pin all the active
 * regions.
 */
static int o2hb_region_pin(const char *region_uuid)
{
	int ret = 0, found = 0;
	struct o2hb_region *reg;
	char *uuid;

	assert_spin_locked(&o2hb_live_lock);

	list_for_each_entry(reg, &o2hb_all_regions, hr_all_item) {
		if (reg->hr_item_dropped || !config_item_name(&reg->hr_item))
			continue;
		uuid = config_item_name(&reg->hr_item);

		/* local heartbeat */
		if (region_uuid) {
			if (strcmp(region_uuid, uuid))
				continue;
			found = 1;
		}

		if (reg->hr_item_pinned || reg->hr_item_dropped)
			goto skip_pin;

		config_item_get(&reg->hr_item);
		spin_unlock(&o2hb_live_lock);

		/* Ignore ENOENT only for local hb (userdlm domain) */
		ret = o2nm_depend_item(&reg->hr_item);
		if (!ret) {
			mlog(ML_CLUSTER, "Pin region %s\n", uuid);
			reg->hr_item_pinned = 1;
		} else {
			if (ret == -ENOENT && found)
				ret = 0;
			else {
				mlog(ML_ERROR, "Pin region %s fails with %d\n",
				     uuid, ret);
				config_item_put(&reg->hr_item);
				spin_lock(&o2hb_live_lock);
				break;
			}
		}
		config_item_put(&reg->hr_item);
		spin_lock(&o2hb_live_lock);
skip_pin:
		if (found)
			break;
	}

	return ret;
}

/*
 * In local heartbeat mode, region_uuid passed matches the dlm domain name.
 * In global heartbeat mode, region_uuid passed is NULL.
 *
 * In local, we only unpin the matching region. In global we unpin all the
 * active regions.
 */
static void o2hb_region_unpin(const char *region_uuid)
{
	struct o2hb_region *reg;
	char *uuid;
	int found = 0;

	assert_spin_locked(&o2hb_live_lock);

	list_for_each_entry(reg, &o2hb_all_regions, hr_all_item) {
		if (reg->hr_item_dropped || !config_item_name(&reg->hr_item))
			continue;
		uuid = config_item_name(&reg->hr_item);
		if (region_uuid) {
			if (strcmp(region_uuid, uuid))
				continue;
			found = 1;
		}

		if (reg->hr_item_pinned) {
			mlog(ML_CLUSTER, "Unpin region %s\n", uuid);
			o2nm_undepend_item(&reg->hr_item);
			reg->hr_item_pinned = 0;
		}
		if (found)
			break;
	}
}

static int o2hb_region_inc_user(const char *region_uuid)
{
	int ret = 0;

	spin_lock(&o2hb_live_lock);

	/* local heartbeat */
	if (!o2hb_global_heartbeat_active()) {
	    ret = o2hb_region_pin(region_uuid);
	    goto unlock;
	}

	/*
	 * if global heartbeat active and this is the first dependent user,
	 * pin all regions if quorum region count <= CUT_OFF
	 */
	o2hb_dependent_users++;
	if (o2hb_dependent_users > 1)
		goto unlock;

	if (bitmap_weight(o2hb_quorum_region_bitmap,
			   O2NM_MAX_REGIONS) <= O2HB_PIN_CUT_OFF)
		ret = o2hb_region_pin(NULL);

unlock:
	spin_unlock(&o2hb_live_lock);
	return ret;
}

static void o2hb_region_dec_user(const char *region_uuid)
{
	spin_lock(&o2hb_live_lock);

	/* local heartbeat */
	if (!o2hb_global_heartbeat_active()) {
	    o2hb_region_unpin(region_uuid);
	    goto unlock;
	}

	/*
	 * if global heartbeat active and there are no dependent users,
	 * unpin all quorum regions
	 */
	o2hb_dependent_users--;
	if (!o2hb_dependent_users)
		o2hb_region_unpin(NULL);

unlock:
	spin_unlock(&o2hb_live_lock);
}

void o2hb_handle_invalid(const char *domain_name)
{
	struct o2hb_region *reg;
	struct o2nm_node *node = NULL;
	struct o2hb_disk_slot *slot;
	struct o2hb_node_event event = {
		.hn_item = LIST_HEAD_INIT(event.hn_item),
	};
	int found = 0;

	spin_lock(&o2hb_live_lock);
	list_for_each_entry(reg, &o2hb_all_regions, hr_all_item) {
		if (reg->hr_item_dropped || !config_item_name(&reg->hr_item))
			continue;
		if (!strncmp(domain_name, config_item_name(&reg->hr_item),
				strlen(config_item_name(&reg->hr_item)))) {
			found = 1;
			break;
		}
	}
	spin_unlock(&o2hb_live_lock);

	if (found == 0) {
		mlog(ML_ERROR, "region %s not exist\n", domain_name);
		return;
	}

	slot = reg->hr_slots[o2nm_this_node()];
	node = o2nm_get_node_by_num(slot->ds_node_num);
	if (!node) {
		mlog(ML_ERROR, "%s: Node not exist!\n", domain_name);
		return;
	}

	mutex_lock(&o2hb_mutex_lock);
	spin_lock(&o2hb_live_lock);
	if (reg->hr_unclean_stop == 1) {
		spin_unlock(&o2hb_live_lock);
		mutex_unlock(&o2hb_mutex_lock);
		o2nm_node_put(node);
		mlog(ML_ERROR, "region %s already invalid\n", domain_name);
		return;
	}
	o2hb_queue_node_event(&event, O2HB_DISK_TIMEOUT_CB,
			node, slot->ds_node_num, config_item_name(&reg->hr_item),
			reg->hr_generation);
	spin_unlock(&o2hb_live_lock);
	o2hb_run_event_list(&event);

	o2hb_disk_timeout(config_item_name(&reg->hr_item));
	mutex_unlock(&o2hb_mutex_lock);
	o2nm_node_put(node);
}
EXPORT_SYMBOL_GPL(o2hb_handle_invalid);

int o2hb_register_callback(const char *region_uuid,
			   struct o2hb_callback_func *hc)
{
	struct o2hb_callback_func *f;
	struct o2hb_callback *hbcall;
	int ret;

	BUG_ON(hc->hc_magic != O2HB_CB_MAGIC);
	BUG_ON(!list_empty(&hc->hc_item));

	hbcall = hbcall_from_type(hc->hc_type);
	if (IS_ERR(hbcall)) {
		ret = PTR_ERR(hbcall);
		goto out;
	}

	if (region_uuid) {
		ret = o2hb_region_inc_user(region_uuid);
		if (ret) {
			mlog_errno(ret);
			goto out;
		}
	}

	down_write(&o2hb_callback_sem);

	list_for_each_entry(f, &hbcall->list, hc_item) {
		if (hc->hc_priority < f->hc_priority) {
			list_add_tail(&hc->hc_item, &f->hc_item);
			break;
		}
	}
	if (list_empty(&hc->hc_item))
		list_add_tail(&hc->hc_item, &hbcall->list);

	up_write(&o2hb_callback_sem);
	ret = 0;
out:
	mlog(ML_CLUSTER, "returning %d on behalf of %p for funcs %p\n",
	     ret, __builtin_return_address(0), hc);
	return ret;
}
EXPORT_SYMBOL_GPL(o2hb_register_callback);

void o2hb_unregister_callback(const char *region_uuid,
			      struct o2hb_callback_func *hc)
{
	BUG_ON(hc->hc_magic != O2HB_CB_MAGIC);

	mlog(ML_CLUSTER, "on behalf of %p for funcs %p\n",
	     __builtin_return_address(0), hc);

	/* XXX Can this happen _with_ a region reference? */
	if (list_empty(&hc->hc_item))
		return;

	if (region_uuid)
		o2hb_region_dec_user(region_uuid);

	down_write(&o2hb_callback_sem);

	list_del_init(&hc->hc_item);

	up_write(&o2hb_callback_sem);
}
EXPORT_SYMBOL_GPL(o2hb_unregister_callback);

/*
 * add region uuid as parameter of function, improve function usability.
 */
int o2hb_check_node_heartbeating_from_callback(u16 node_num, const char *uuid)
{
	unsigned long testing_map[BITS_TO_LONGS(O2NM_MAX_NODES)];

	memset(testing_map, 0, sizeof(testing_map));
	o2hb_fill_node_map_from_callback(testing_map, sizeof(testing_map), uuid);
	if (!test_bit(node_num, testing_map)) {
		mlog(ML_HEARTBEAT,
		     "%s: node (%u) does not have heartbeating enabled.\n",
		     uuid ? uuid : "o2hb", node_num);
		return 0;
	}

	return 1;
}
EXPORT_SYMBOL_GPL(o2hb_check_node_heartbeating_from_callback);

/*
 * this is just a hack until we get the plumbing which flips file systems
 * read only and drops the hb ref instead of killing the node dead.
 */
void o2hb_stop_all_regions(void)
{
	struct o2hb_region *reg;

	mlog(ML_ERROR, "o2hb: stopping heartbeat on all active regions.\n");

	spin_lock(&o2hb_live_lock);

	list_for_each_entry(reg, &o2hb_all_regions, hr_all_item) {
		reg->hr_unclean_stop = 1;
		reg->hr_fence = 1;
	}

	spin_unlock(&o2hb_live_lock);
}
EXPORT_SYMBOL_GPL(o2hb_stop_all_regions);

int o2hb_get_all_regions(char *region_uuids, u8 max_regions)
{
	struct o2hb_region *reg;
	int numregs = 0;
	char *p;

	spin_lock(&o2hb_live_lock);

	p = region_uuids;
	list_for_each_entry(reg, &o2hb_all_regions, hr_all_item) {
		if (reg->hr_item_dropped || !config_item_name(&reg->hr_item))
			continue;
		mlog(0, "Region: %s\n", config_item_name(&reg->hr_item));
		if (numregs < max_regions) {
			memcpy(p, config_item_name(&reg->hr_item),
			       O2HB_MAX_REGION_NAME_LEN);
			p += O2HB_MAX_REGION_NAME_LEN;
		}
		numregs++;
	}

	spin_unlock(&o2hb_live_lock);

	return numregs;
}
EXPORT_SYMBOL_GPL(o2hb_get_all_regions);

int o2hb_global_heartbeat_active(void)
{
	return (o2hb_heartbeat_mode == O2HB_HEARTBEAT_GLOBAL);
}
EXPORT_SYMBOL(o2hb_global_heartbeat_active);

int o2hb_disable_fence(void)
{
	return disable_fence;
}
EXPORT_SYMBOL(o2hb_disable_fence);

int o2hb_is_unclean_stop(const char *uuid)
{
	struct o2hb_region *reg;
	int unclean_stop = 0;

	spin_lock(&o2hb_live_lock);
	list_for_each_entry(reg, &o2hb_all_regions, hr_all_item) {
		if (reg->hr_item_dropped || !config_item_name(&reg->hr_item))
			continue;

		if (!strncmp(uuid, config_item_name(&reg->hr_item),
				strlen(config_item_name(&reg->hr_item)))) {
			unclean_stop = reg->hr_unclean_stop;
			break;
		}
	}
	spin_unlock(&o2hb_live_lock);

	return unclean_stop;
}
EXPORT_SYMBOL_GPL(o2hb_is_unclean_stop);

struct block_device *o2hb_get_block_device(const char *domain_name)
{
	struct o2hb_region *reg;
	struct block_device *bdev = NULL;

	spin_lock(&o2hb_live_lock);

	list_for_each_entry(reg, &o2hb_all_regions, hr_all_item) {
		if (reg->hr_item_dropped || !config_item_name(&reg->hr_item))
			continue;

		mlog(0, "Region: %s\n", config_item_name(&reg->hr_item));
		if (!strncmp(domain_name, config_item_name(&reg->hr_item),
				strlen(config_item_name(&reg->hr_item)))) {
			bdev = reg->hr_bdev;
			break;
		}
	}

	spin_unlock(&o2hb_live_lock);

	return bdev;
}
EXPORT_SYMBOL_GPL(o2hb_get_block_device);

/*
 * return value: 0  - dlm
 *               1  - adl
 */
int o2hb_check_heartbeating_type(const char *domain_name)
{
	struct o2hb_region *reg;
	int lock_type = 0;

	spin_lock(&o2hb_live_lock);

	list_for_each_entry(reg, &o2hb_all_regions, hr_all_item) {
		if (reg->hr_item_dropped || !config_item_name(&reg->hr_item))
			continue;

		mlog(0, "Region: %s\n", config_item_name(&reg->hr_item));
		if (!strncmp(domain_name, config_item_name(&reg->hr_item),
				strlen(config_item_name(&reg->hr_item)))) {
			lock_type = reg->lock_type_adl;
			break;
		}
	}

	spin_unlock(&o2hb_live_lock);

	return lock_type;
}
EXPORT_SYMBOL_GPL(o2hb_check_heartbeating_type);

bool o2hb_conn_normal(const char *domain_name)
{
	struct o2hb_region *reg;
	bool ret = false;
	unsigned long flags;

	spin_lock(&o2hb_live_lock);

	list_for_each_entry(reg, &o2hb_all_regions, hr_all_item) {
		if (reg->hr_item_dropped || !config_item_name(&reg->hr_item))
			continue;

		if (!strncmp(domain_name, config_item_name(&reg->hr_item),
				strlen(config_item_name(&reg->hr_item)))) {
			spin_lock_irqsave(&reg->hr_lock, flags);
			ret = o2quo_conn_normal(reg->hr_live_node_bitmap, reg->hr_qs_effect_nodes);
			spin_unlock_irqrestore(&reg->hr_lock, flags);
			mlog(ML_HEARTBEAT, "%s: network state is %s\n",
					config_item_name(&reg->hr_item),
					ret ? "normal" : "abnormal");
			break;
		}
	}

	spin_unlock(&o2hb_live_lock);
	return ret;
}
EXPORT_SYMBOL_GPL(o2hb_conn_normal);

bool o2hb_is_in_domain(const char *domain_name, u16 node_num)
{
	struct o2hb_region *reg;
	bool ret = false;
	unsigned long flags;

	spin_lock(&o2hb_live_lock);
	list_for_each_entry(reg, &o2hb_all_regions, hr_all_item) {
		if (reg->hr_item_dropped || !config_item_name(&reg->hr_item))
			continue;

		if (!strncmp(domain_name, config_item_name(&reg->hr_item),
				strlen(config_item_name(&reg->hr_item)))) {
			spin_lock_irqsave(&reg->hr_lock, flags);
			ret = test_bit(node_num, reg->hr_live_node_bitmap);
			spin_unlock_irqrestore(&reg->hr_lock, flags);
			mlog(ML_HEARTBEAT, "%s: node %u is %s in domain\n",
					config_item_name(&reg->hr_item),
					node_num, ret ? "" : "not");
			break;
		}
	}
	spin_unlock(&o2hb_live_lock);

	return ret;
}
EXPORT_SYMBOL_GPL(o2hb_is_in_domain);
